Here is the code for reference. For background, there is a class
called OMRUser which is a user class, and there is Location3 class.
There is a one to many owned relationship between a user and locations
snippet of USER class:
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class OmrUser {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
@Persistent
private User gaeuser;
@Persistent(mappedBy = "omruser")
@Element(dependent = "true")
private List<Location3> userlocs;
@Persistent(mappedBy = "omruser")
@Element(dependent = "true")
private List<Driver> userdrivers;
@Persistent
private String accounttype;
snippet of Location class:
@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Location3 implements Comparable<Location3> {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
@Persistent
private OmrUser omruser;
snippet of servlet to save locations
PersistenceManager pm = PMF.get().getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
String select_query = "select from " + OmrUser.class.getName();
Query query = pm.newQuery(select_query);
query.setFilter("gaeuser == paramAuthor");
query.declareParameters("java.lang.String paramAuthor");
List<OmrUser> omruserquery = (List<OmrUser>)
pm.newQuery(query).execute(user);
if(omruserquery.iterator().hasNext()) {
OmrUser thisomruser =
omruserquery.iterator().next();
if(thisomruser.getNumlocs()<MaxSaveLocs){
thisomruser.addSingleLoc(location);
location.setOmruser(thisomruser);
pm.makePersistent(location);
log.info("Adding Location to existing OMR
User");
} else {
msg = "You have exceeded the maximum
number of locations you can
save, please delete a location first.";
log.info("FAIL - Exceed max num locs");
}
} else {
OmrUser newuser = new OmrUser(user, "Basic");
newuser.addSingleLoc(location);
pm.makePersistent(newuser);
location.setOmruser(newuser);
log.info("Created new user " +
newuser.getGaeuser().getEmail());
}
tx.commit();
} finally {
if (tx.isActive()) {
tx.rollback();
}
}
On Jun 15, 9:39 pm, shaz <[email protected]> wrote:
> Hi, I have a functioning app and recently have had intermittent
> problems writing to the datastore. I did not make any relevant code
> changes, however in the last few days my attempts to write to the
> datastore sometimes work and sometimes don't.
>
> I am trying to save an object that is in a many to one relationship
> with an existing persisted parent. So, the logic works like this:
>
> - Parent pulled from the
> datastore
> - Child created / instantiated
> using constructor
>
> - Parent.addSingleChild(child); //
> the "addSingleChild" method
> just adds the object argument to the collection of children
>
> - child.setParent(Parent); // sets
> the Parent object to the
> parent field
>
> I am using transactions as explained in the documentation ending with
> "finally {if (tx.isActive()) {tx.rollback(); } }"
>
> When the servlet is called, the parent is called from the datastore
> and the child object is created and added to the many to one mapping
> to the pre-existing parent.
>
> The child should automatically be persisted, since the parent is
> already persistent, and the child is added to the collection of
> children that map to the parent. And it worked this way in the
> past. However, to be sure, i did add a pm.makePersistent(child).
> Doesn't seem to help, still have the intermittent problem.
>
> Any suggestions would be appreciated, and if you need to see the
> actual code I can post. Thanks
>
> appid is omrtest
--
You received this message because you are subscribed to the Google Groups
"Google App Engine for Java" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-appengine-java?hl=en.