I'm writing a small application using GWT and JDOs to store persistent data. I've been trying to write a function that updates multiple JDO objects based on a query. Its body looks something like this:
PersistenceManager pm = getPersistenceManager();
try {
q = pm.newQuery(MyObject.class, query);
List<MyObject> objects = (List<MyObject>) q.execute();
for (MyObject object: objects) {
object.setMyField(newValue);
}
System.out.println(objects); //[1]
} finally {
pm.close();
}
If I look at the output of the print statement at [1], the objects all have the correctly updated values. When I make an RPC to retrieve the updated objects, however, the values are the same as they were before the update method was called. If I check the datastore in the admin panel, the updates also do not appear.
All the other persistence behaviour in my application works fine (e.g. adding new objects, removing objects, querying objects), but I can't seem to figure out what's wrong here. Most of the resources I've found (like http://code.google.com/appengine/docs/java/datastore/jdo/creatinggettinganddeletingdata.html#Updating_an_Object) suggest that just having the PersistenceManager open should persist any changes you make to retrieved objects.
Any suggestions? I am truly baffled.
Thank you!
SOLUTION
PersistenceManager pm = getPersistenceManager();
try {
q = pm.newQuery(MyObject.class, query);
List<MyObject> objects = (List<MyObject>) q.execute();
for (MyObject object: objects) {
object.setMyField(newValue);
JDOHelper.makeDirty(object, myField);
}
} finally {
pm.close();
}