更新的 JDO 对象不会保留在 GWT 中
我正在编写一个使用 GWT 和 JDO 来存储持久数据的小型应用程序。我一直在尝试编写一个根据查询更新多个 JDO 对象的函数。它的主体看起来像这样:
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();
}
如果我查看 [1] 中 print 语句的输出,所有对象都具有正确更新的值。然而,当我进行 RPC 来检索更新的对象时,这些值与调用更新方法之前的值相同。如果我在管理面板中检查数据存储,更新也不会出现。
我的应用程序中的所有其他持久性行为都工作正常(例如添加新对象、删除对象、查询对象),但我似乎无法弄清楚这里出了什么问题。我找到的大部分资源(例如 http:// /code.google.com/appengine/docs/java/datastore/jdo/creatinggettinganddeletingdata.html#Updating_an_Object)建议只使用 PersistenceManager open 应该保留您对检索到的对象所做的任何更改。
有什么建议吗?我真的很困惑。
谢谢你!
解决方案
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();
}
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
更新 JDO 的正确方法是使用事务,例如(无需正确处理)
我建议您对其他所有其他更新场景(包括添加/删除操作)执行此操作。
The proper way to update JDOs is to use a transaction, e.g. (w/o proper handling)
I'd recommend you do that for other all other update scenario including add/delete operations.