Search code examples
javajdo

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


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,
            "[email protected]");

        // If you're using transactions, you can call
        // pm.setDetachAllOnCommit(true) before committing to automatically
        // detach all objects without calls to detachCopy or detachCopyAll.
        detached = pm.detachCopy(employee);
    } finally {
        pm.close();
    }
    return detached;
}

Solution

  • 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?