How to create an Object in a specific Entity Group with JPA? (Google AppEngine Java) -
i want use transactions in gae-j jpa.
without jpa should be:
entity child= new entity("child", "parentkey");
but how jpa?
@entity public class parent{ @id @generatedvalue(strategy = generationtype.identity) private key id; private string text; } @entity public class child{ @id @generatedvalue(strategy = generationtype.identity) private key id; private parent parent; private string text; }
trying...
parent parent = new parent(); em.persist(parent); em.refresh(parent); child child = new child(); child.setparent(parent); em.persist(child);
this doesn't work:
org.datanucleus.store.appengine.datastorerelationfieldmanager$childwithoutparentexception: detected attempt establish child(130007) parent of parent(132001) entity identified child(132001) has been persisted without parent. parent cannot established or changed once object has been persisted.
that sounds bit front... blockhead? or there easy way?
thanks!
kay... little error in first attempt.
this should work:
@entity public class parent{ @id @generatedvalue(strategy = generationtype.identity) private key id; private string text; @onetomany(targetentity=child.class, mappedby="parent", fetch=fetchtype.lazy) private set<child> children = new hashset<child>(); } @entity public class child{ @id @generatedvalue(strategy = generationtype.identity) private key id; @manytoone(fetch=fetchtype.lazy, targetentity=parent.class) private parent parent; private string text; }
Comments
Post a Comment