java - Why doesn't my owned one-to-many relationship get persisted to the GAE datastore? -
java - Why doesn't my owned one-to-many relationship get persisted to the GAE datastore? -
i have 2 classes defined , mapped gae datastore - person class , locationstamp class, represents single latitude/longitude combination along timestamp. there owned one-to-many mapping between person , locationstamp, implemented on similar lines a forum post on one-to-many relationships.
in dao person, have next method:
public locationstamp addlocationstampforcurrentuser(locationstamp ls) { persistencemanager pm = getpersistencemanager(); person p = getprofileforcurrentuser(); p.getlocationstamps().add(ls); pm.currenttransaction().begin(); seek { pm.makepersistent(p); pm.currenttransaction().commit(); homecoming ls; } { if (pm.currenttransaction().isactive()) { pm.currenttransaction().rollback(); } pm.close(); } } when seek using method add together entry object's collection of locationstamp entities, the relationship not persisted. later query relevant person object returns right object email address, locationstamps list empty. moreover, info viewer app engine server not show locationstamp entities (and there isn't column shown locationstamps in table person.
i followed instructions in forum post carefully, i'm not sure if i'm missing something.
here entities:
@persistencecapable(identitytype = identitytype.application, detachable = "true") public class person { @primarykey @persistent(valuestrategy = idgeneratorstrategy.identity) private key key; @persistent private string emailaddress; @persistent private string name; @persistent(mappedby = "person") @element(dependent = "true") @order(extensions = @extension(vendorname = "datanucleus", key = "list-ordering", value = "timestamp desc")) private final list<locationstamp> locationstamps = new arraylist<locationstamp>(); // ... getters , setters ... } and
@persistencecapable(identitytype = identitytype.application, detachable = "true") public class locationstamp { @primarykey @persistent(valuestrategy = idgeneratorstrategy.identity) private key key; @persistent private double latitude; @persistent private double longitude; @persistent private date timestamp; @persistent private boolean automatic; @persistent private person person; // ... getters , setters ... } since they're used in above code, here definitions couple of other methods:
public person getprofileforcurrentuser() { persistencemanager pm = getpersistencemanager(); seek { key k = getkeyforemailaddress(getcurrentuseremail()); homecoming pm.getobjectbyid(person.class, k); } grab (jdoobjectnotfoundexception e) { // create new profile if new 1 isn't found homecoming updateprofileforcurrentuser(new person()); } { pm.close(); } } public person updateprofileforcurrentuser(person p) { p.setemailaddress(getcurrentuseremail()); homecoming update(p); } public person update(person p) { persistencemanager pm = getpersistencemanager(); seek { pm.currenttransaction().begin(); key k = getkeyforemailaddress(p.getemailaddress()); p.setkey(k); pm.makepersistent(p); pm.currenttransaction().commit(); homecoming p; } { if (pm.currenttransaction().isactive()) { pm.currenttransaction().rollback(); } pm.close(); } } private key getkeyforemailaddress(string emailaddress) { homecoming keyfactory.createkey(person.class.getsimplename(), emailaddress); } private static persistencemanager getpersistencemanager() { // pmf singleton class returns instance of persistencemanagerfactory homecoming pmf.get().getpersistencemanager(); }
when have list in persistence object, load of list lazy operation. if close persistencemanager before load list, list not loaded.
try using:
public person getprofileforcurrentuser() { persistencemanager pm = getpersistencemanager(); seek { key k = getkeyforemailaddress(getcurrentuseremail()); person p = pm.getobjectbyid(person.class, k); p.getlocationstamps().size(); homecoming p; } grab (jdoobjectnotfoundexception e) { // create new profile if new 1 isn't found homecoming updateprofileforcurrentuser(new person()); } { pm.close(); } } java google-app-engine gae-datastore jdo datanucleus
Comments
Post a Comment