Benx Blog

十一月 12, 2014

Diigo Diary 11/12/2014

Filed under: Diigo Diary — benxshen @ 8:30 上午
    • JPA defines two main states for an object for a given persistence context, managed and detached.


      A managed object is one that was read in the current persistence context (EntityManager/JTA transaction). A managed object is registered with the persistence context and the persistence context will track changes to that object and maintain its object identity. If the same object is read again, in the same persistence context, or traversed through another managed object’s relationship, the same identical (==) object will be returned. Calling persist on a new object will also make it become managed. Calling merge on a detached object will return the managed copy of the object. An object should never be managed by more than one persistence context. An object will be managed by its persistence context until the persistence context is cleared through clear, or the object is forced to be detached through detach. A removed object will no longer be managed after a flush or commit. On a rollback, all managed objects will become detached. In a JTA managed EntityManager all managed objects will be detached on any JTA commit or rollback.

    • merge does not directly update the object into the database, it merges the changes into the persistence context (transaction)
    • When the transaction is committed, or if the persistence context is flushed, then the object will be updated in the database.
    • The EntityManager.remove() operation is used to delete an object from the database. remove does not directly delete the object from the database, it marks the object to be deleted in the persistence context (transaction). When the transaction is committed, or if the persistence context is flushed, then the object will be deleted from the database.
    • The EntityManager.refresh() operation is used to refresh an object’s state from the database. This will revert any non-flushed changes made in the current transaction to the object, and refresh its state to what is currently defined on the database.
    • Refresh must be called on a managed object, so you may first need to find the object with the active EntityManager if you have a non-managed instance.
    • The EntityManager.getReference() operation is used to obtain a handle to an object without requiring it to be loaded.
    • The content of the specified detached entity object is copied into an existing managed entity object with the same identity (i.e. same type and primary key). If the EntityManager does not manage such an entity object yet a new managed entity object is constructed. The detached object itself, however, remains unchanged and detached.

Posted from Diigo. The rest of my favorite links are here.

發表迴響 »


RSS feed for comments on this post. TrackBack URI


在下方填入你的資料或按右方圖示以社群網站登入: 標誌

您的留言將使用 帳號。 登出 /  變更 )

Google+ photo

您的留言將使用 Google+ 帳號。 登出 /  變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 /  變更 )


您的留言將使用 Facebook 帳號。 登出 /  變更 )


連結到 %s


%d 位部落客按了讚: