简体   繁体   English

JDO为什么认为这个分离的对象是干净的?

[英]Why does JDO think this detached object is clean?

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. 我正在尝试通过创建一个小型Web应用程序来学习JDO(同时又了解其GAE和Spring的复杂性),并且在获取更新的域对象以将其持久化回数据库方面遇到困难。 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() . 一旦用户进行了更改并将表单发布回应用程序,我将再次从数据库(分离的)中获取实体,更新其属性,然后调用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: 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 Update Method: 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();
    }
}

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. 现在,当我进入update方法时,我已经通过检查其hashCode()来证明自己正在使用读取的同一对象,我已经使用域对象的setter方法更改了值,甚至将更改后的值打印到控制台以确保已完成,并且JDOHelper.isDirty()仍然返回false,因此所有更改都不会持久化回到数据库。 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! 感谢您帮助JDO初学者!

JDOHelper.isDirty is for managed objects. JDOHelper.isDirty用于托管对象。 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); DataNucleus提供了自己的帮助器方法,以便在分离时获取脏字段,因为逻辑是特定于实现的String [] dirtyFieldNames = NucleusJDOHelper.getDetachedObjectDirtyFields(obj,pm);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM