Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I setup a ManyToOne relationship using JPA with AppEngine?

I've been trying to get a relationship working with 2 entities in AppEngine, using JPA, and am currently running into this error:

java.io.IOException: com.google.appengine.repackaged.org.codehaus.jackson.map.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain

My entities look like this:

@Entity
public class MyUser {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Key key;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "user", cascade = CascadeType.ALL)
    private List<MyMessage> messages;
}

and this:

@Entity
public class MyMessage {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Key key;

    @ManyToOne(optional=false)
    private MyUser user;

}

The user already exists, and here is where I'm inserting a new message and get the recursion error:

EntityManager mgr = getEntityManager();
MyUser myuser = mgr.find(MyUser.class, KeyFactory.createKey("MyUser", user.getEmail()));
mymessage.setUser(myuser);
myuser.addMessage(mymessage);
mgr.persist(myuser);
mgr.persist(mymessage);

How am I supposed to setup this relationship within JPA and AppEngine guidelines? Thank you!

UPDATE

My problem was involving Jackson, not JPA. The JPA relationship is fine, but I needed to remove the relationship and manage it through the code as it was causing infinite recursion in serializing messages referring to users referring to messages and so on. I've also had to make sure that I annotated the user property in MyMessage as @Transient to avoid persistence complaining about persisting a parent owned by a child which already existed.

like image 904
piusvelte Avatar asked Dec 18 '25 18:12

piusvelte


1 Answers

I'm unaware of a reasonable way for Endpoints to serialize these classes. The JSON resulting from your current code would look smiilar to the following:

// 1
{
  "key": "foo",
  "messages": [
    {
      "key": "bar",
      "user": {
        // repeat 1
    },
  // and so on...
}

Your best bet is to define a class (or classes) to send over the wire, instead of your JPA entities, which define JSON an infinite number of levels deep.

like image 129
Dan Holevoet Avatar answered Dec 21 '25 02:12

Dan Holevoet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!