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

發表迴響

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

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 變更 )

Twitter picture

You are commenting using your Twitter account. Log Out / 變更 )

Facebook照片

You are commenting using your Facebook account. Log Out / 變更 )

Google+ photo

You are commenting using your Google+ account. Log Out / 變更 )

連結到 %s

在 WordPress.com 建立免費網站或網誌.

%d 位部落客按了讚: