Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I setup a ManyToOne relationship using JPA with AppEngine?
    text
    copied!<p>I've been trying to get a relationship working with 2 entities in AppEngine, using JPA, and am currently running into this error:</p> <pre><code>java.io.IOException: com.google.appengine.repackaged.org.codehaus.jackson.map.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain </code></pre> <p>My entities look like this:</p> <pre><code>@Entity public class MyUser { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Key key; @OneToMany(fetch = FetchType.LAZY, mappedBy = "user", cascade = CascadeType.ALL) private List&lt;MyMessage&gt; messages; } </code></pre> <p>and this:</p> <pre><code>@Entity public class MyMessage { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Key key; @ManyToOne(optional=false) private MyUser user; } </code></pre> <p>The user already exists, and here is where I'm inserting a new message and get the recursion error:</p> <pre><code>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); </code></pre> <p>How am I supposed to setup this relationship within JPA and AppEngine guidelines? Thank you!</p> <p><strong>UPDATE</strong></p> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload