views:

631

answers:

1

Hi, i am experimenting with the Google App Engine, and the persist option JDO. I would like to know if it is possible to map a transient object to a persist object? Or something to update the persistent object with the use of an transient object?

I the coding examples i see the following piece of code to update objects:

public void updateEmployeeTitle(User user, String newTitle) {
PersistenceManager pm = PMF.get().getPersistenceManager();
try {
    Employee e = pm.getObjectById(Employee.class, user.getEmail());
    if (titleChangeIsAuthorized(e, newTitle) {
        e.setTitle(newTitle);
    } else {
        throw new UnauthorizedTitleChangeException(e, newTitle);
    }
} finally {
    pm.close();
}
}

But this is not what i want, does anybody know if i can update the whole object like JPA does: object.update();

So i would like something like this:

public User update(User u) {
 PersistenceManager pm = PMF.get().getPersistenceManager();
 User usr;
 try {
  usr = pm.getObjectById(User.class, u.getId());
  // copy transient object u to persist object usr.
                    // on update of usr all changes in object u are persistent.

 } finally {
  pm.close();
 }

 return u;
}
+1  A: 

A "transient" object has no "identity" so there is no way to locate it in a datastore. Think carefully if you want to use transient objects, or whether it would be better to just use detached objects. JPA uses the equivalent of a "detached" object. JDO can do that too and then you just call pm.makePersistent(detachedObj);

--Andy (DataNucleus)

DataNucleus
makePersistent does not work if the object already has the ID set (e.g. when previously read from the datastore, or when populated from a web form).
pjesi
If the object is read from the datastore and is in the same transaction then there is no point in calling makePersistent, any changes are automatically put in the datastore.If the object is read from the datastore and then detached and you change something then calling makePersistent certainly does "work" since it attaches the changes. If you have specific problems then i'd suggest you report them to Google's appengine group
DataNucleus