2010-02-10 66 views
1

我對分離副本的理解是,它會創建對象的副本,以便您可以在沒有PersistenceManager注意的情況下對其進行更改。JDO - 在將模型對象傳遞給視圖之前,需要調用DetachCopy嗎?

因爲我接近用來傳遞模型對象視圖之前我的PersistenceManager,我就不會沿着通過它我會之前調用像detachCopy或makeTransient什麼?

我看着這些示例雖然叫它... 這是我看着例如,從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; 
} 

回答

1

您可以自動的對象使用PMF道具分離,或分離它們的副本如示例所示,手動。現在是什麼問題?

+0

所以,如果我也不detachCopy或makeTransient它會拋出一個異常關閉PM之前? – Kyle 2010-02-10 19:39:43

+0

究竟會拋出異常嗎?如果你不makeTransient,或detachCopy或有PMF道具設置然後對象遷移到HOLLOW狀態,按照JDO規範 – DataNucleus 2010-02-10 20:27:36

+0

按照http://www.datanucleus.org/products/accessplatform/jdo/object_lifecycle.html – DataNucleus 2010-02-11 06:57:49

1

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

分離對象保留其數據存儲區的實體的ID。分離 對象應該用於要更新對象的位置,並在稍後將它們附加到 (更新數據存儲區中的關聯對象。如果 想要使用自己的 身份創建數據存儲區中對象的副本,則應該使用makeTransient的detachCopy。