为什么 JDO 认为这个分离对象是干净的?
我正在尝试通过创建一个小型 Web 应用程序来学习 JDO(同时学习其 GAE 和 Spring 的复杂性),但在获取更新的域对象以保留回数据库时遇到了困难。我最初从数据库中获取实体并将其分离,以便我可以将其显示给用户并允许他们更改它。用户进行更改并将表单发布回应用程序后,我再次从数据库中获取实体(分离),更新其属性,然后调用 pm.makePersistent()
。缩写代码如下:
用户域对象:
@PersistenceCapable(detachable="true")
public class User extends BaseEntity {
@Persistent
private String firstName = "";
@Persistent
private String middleInitial = "";
@Persistent
private String lastName = "";
}
DAO 读取方法:
public User read(Key key) throws DataException {
PersistenceManager pm = PMF.get().getPersistenceManager();
User pkg, detached = null;
try {
pkg = (User) pm.getObjectById(User.class, key);
detached = pm.detachCopy(pkg);
detached.setIsAlreadyInDB(true);
}
catch (Exception e) {
throw new DataException("An error occured trying to read the User object. Details:\n" + e.getMessage());
}
finally {
pm.close();
}
return detached;
}
DAO 更新方法:
private void update(User pkg) throws DataException {
PersistenceManager pm = PMF.get().getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
pm.makePersistent(pkg);
tx.commit();
}
finally {
if (tx.isActive()) tx.rollback();
pm.close();
}
}
现在,当我深入了解更新方法时,我已经向自己证明,我正在通过检查读取相同的对象它的 hashCode(),我使用域对象的 setter 方法更改了一个值,我什至将更改后的值打印到控制台以确保它已完成,并且 JDOHelper.isDirty () 仍然返回 false,因此没有任何更改会保留回数据库。 对我缺少什么或者我是否从错误的角度处理这个问题有什么想法?感谢您对 JDO 初学者的帮助!
I am trying to learn JDO (and at the same time its GAE and Spring intricacies) by creating a small web app, and am having trouble getting updated domain objects to persist back to the database. I initially grab the entity from the DB and detach it so that I can show it to the user and allow them to change it. Once the user has made the changes and posts the form back to the app, I again grab the entity from the DB (detached), update its properties, and then call a pm.makePersistent()
. The abbreviated code is as follows:
User Domain Object:
@PersistenceCapable(detachable="true")
public class User extends BaseEntity {
@Persistent
private String firstName = "";
@Persistent
private String middleInitial = "";
@Persistent
private String lastName = "";
}
DAO Read Method:
public User read(Key key) throws DataException {
PersistenceManager pm = PMF.get().getPersistenceManager();
User pkg, detached = null;
try {
pkg = (User) pm.getObjectById(User.class, key);
detached = pm.detachCopy(pkg);
detached.setIsAlreadyInDB(true);
}
catch (Exception e) {
throw new DataException("An error occured trying to read the User object. Details:\n" + e.getMessage());
}
finally {
pm.close();
}
return detached;
}
DAO Update Method:
private void update(User pkg) throws DataException {
PersistenceManager pm = PMF.get().getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
pm.makePersistent(pkg);
tx.commit();
}
finally {
if (tx.isActive()) tx.rollback();
pm.close();
}
}
Now when I get down into the update method, I've proven to myself that I'm working with just the same object from my read via inspecting its hashCode()
, I've changed a value using the domain object's setter method, I've even printed the changed value to the console to make sure it's getting done, and JDOHelper.isDirty()
still returns false, and therefore none of the changes get persisted back to the database.
Any thoughts on what I'm missing or if I'm approaching this from the wrong angle? Thank you for helping out a JDO beginner!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
JDOHelper.isDirty 用于托管对象。分离的对象不受管理。 DataNucleus 提供了自己的辅助方法来在分离时获取脏字段,因为逻辑是特定于实现的
String[] dirtyFieldNames = NucleusJDOHelper.getDetachedObjectDirtyFields(obj, pm);
JDOHelper.isDirty is for managed objects. A detached object is not managed. DataNucleus provides a helper method of its own to get the dirty fields while detached since the logic is implementation-specific
String[] dirtyFieldNames = NucleusJDOHelper.getDetachedObjectDirtyFields(obj, pm);