JDO for GAE:更新查询返回的对象
有一个可持久的类Project,它的每个实例都有Version类型的对象列表(Project和Version类之间拥有一对多关系)。
我通过查询从数据存储中获取多个版本对象,更改它们并尝试保存:
PersistenceManager pm = PMF.get().getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Query q = pm.newQuery(Version.class, "... filters here ...");
q.declareParameters(" ... parameters here ...");
List<Version> versions = (List<Version>)q.execute(... parameters here ...);
if (versions.size() > 0) {
for (Version version : versions) {
version.setOrder(... value here ...);
}
pm.makePersistentAll(versions);
}
tx.commit();
return newVersion.toVersionInfo();
} finally {
pm.close();
}
一切都执行没有错误,查询实际上返回多个对象,属性在运行时版本列表中设置正确,但对象属性未在数据存储中更新。
一般来说,据我了解,即使不调用 也应该保存版本
pm.makePersistentAll(versions);
,因为对象属性是在 pm.close() 之前设置的,但如果也省略此行,则不会保存任何内容。
同时,如果我使用 pm.getObjectById() 方法检索 Project 类型的实例(它拥有许多 Version 类型的实例),并遍历循环中的所有相关 Version 对象,则所有更改都会正确保存(无需调用 pm.getObjectById() 方法)。 makePersistent() 方法)。
问题是,这种更新对象的方式有什么问题吗?为什么数据存储中的版本对象属性未更新?
我在 JDO 和 GAE 文档中都找不到任何有用的东西。
There is persistable class Project, each instance of which has list of objects of Version type (owned one-to-many relation between Project and Version classes).
I'm getting several Version objects from datastore with query, change them and try to save:
PersistenceManager pm = PMF.get().getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
Query q = pm.newQuery(Version.class, "... filters here ...");
q.declareParameters(" ... parameters here ...");
List<Version> versions = (List<Version>)q.execute(... parameters here ...);
if (versions.size() > 0) {
for (Version version : versions) {
version.setOrder(... value here ...);
}
pm.makePersistentAll(versions);
}
tx.commit();
return newVersion.toVersionInfo();
} finally {
pm.close();
}
Everything is executed without errors, query actually returns several objects, properties are set correctly in runtime versions list, but objects properties are not updated in datastore.
Generally, as far as I understand, versions should be saved even without calling
pm.makePersistentAll(versions);
, since object properties are set before pm.close(), but nothing is saved, if this row is omitted, as well.
At the same time, if I retrieve instance of type Project (which owns many instances of type Version) with pm.getObjectById() method, and walk through all related Version objects in the loop, all changes are saved correctly (without calling pm.makePersistent() method).
The question is, what's wrong with such way of updating objects? Why Version object properties are not updated in datastore?
I could not find anything helpful neither in JDO nor in GAE documentation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
感谢您对 DataNucleus 日志的建议以及 Peter Recore 的同情:)
坦率地说,我错过了问题中的一些重要要点。
实际上,在
和
之间,我正在检索 Project 实例,并且在 Version 实例更新循环之后,我将保留另一个 Version 对象。
所以,我实际上检索了一些版本列表两次,并且根据日志中的提交顺序,项目实例也被保存了两次。第二次保存操作覆盖了第一次保存操作。
我改变了操作顺序,并得到了预期的行为。
Thanks for advice about logs from DataNucleus and sympathy from Peter Recore :)
Frankly, I missed few important points in my question.
Actually,between
and
I am retrieving Project instance, and after Version instances updating loop I am persisting some one more Version object.
So, I actually retrieved some of versions list twice, and according to committing order in logs, Project instance was saved twice, as well. The second save operation overwritten the first one.
I changed the order of operations, and got expected behavior.