jpa - Self referencing @OneToMany -
i have following class
@entity public class comment extends base { @manytoone(cascade = cascadetype.all, fetch = fetchtype.eager) private comment inreplyto; @onetomany(mappedby = "inreplyto", cascade = cascadetype.all, fetch = fetchtype.eager) private collection<comment> replies; public comment() { } public collection<comment> getreplies() { return replies; } public void setreplies(collection<comment> comments) { this.replies = comments; } public comment getinreplyto() { return inreplyto; } public void setinreplyto(comment inreplyto) { this.inreplyto = inreplyto; } }
adding new comment , setting inreplyto works , comment saved db. inreplyto comment has replies field null.
also doing
c.setreplies(new arraylist<comment>()); c.getreplies().add(x); repo.save(c);
results in error
org.datanucleus.store.types.incompatiblefieldtypeexception: incompatible type requested field "comment.replies" : java.util.collection should java.util.collection
any idea?
you should set both parts of relationship:
c.setreplies(new arraylist<comment>()); c.getreplies().add(x); c.setinreplyto(x);//this imporant repo.save(c);
if not work, bug in datanucleus: try report , put link somewhere here.
Comments
Post a Comment