开发者

JDO - Do I need to call DetachCopy on my model object before passing it to a view?

开发者 https://www.devze.com 2022-12-19 15:57 出处:网络
My understanding of detach copy is that it makes a copy of your object so that you can make changes to it without the PersistenceManager noticing.

My understanding of detach copy is that it makes a copy of your object so that you can make changes to it without the PersistenceManager noticing.

Since I close my PersistenceManager before passing the model object to the view to be used, I wouldn't have to call anything like detachCopy or makeTransient before passing it along would I?

The examples I looked at do call it though... This is the example I looked at from http://code.google.com/appengine/docs/java/datastore/creatinggettinganddeletingdata.html:

public Employee getEmployee(User user) {
    PersistenceManager pm = PMF.get().getPersistenceManager();
    Employee employee, detached = null;
    try {
        employee = pm.getObjectById(Employee.class,
            "Alfred.Smith@example.com");

        // If you're using transactions, you can call
        // pm.setDetachAllOnCommit(true) before committing to automatically
        // detach all objects without calls to detachCopy or detachCopyAll.
        detache开发者_运维百科d = pm.detachCopy(employee);
    } finally {
        pm.close();
    }
    return detached;
}


You can have objects detached automatically using the PMF prop, or detach copies of them manually, as the example says. Now what was the question?


From http://db.apache.org/jdo/attach_detach.html

A detached object retains the id of its datastore entity. Detached objects should be used where you want to update the objects and attach them later (updating the associated object in the datastore. If you want to create copies of the objects in the datastore with their own identities you should use makeTransient instead of detachCopy.

0

精彩评论

暂无评论...
验证码 换一张
取 消