Diigo Diary 11/12/2014
Java Persistence/Persisting – Wikibooks, open books for an open world
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
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.
operation is used to delete an object from the database.
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.
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.
operation is used to obtain a handle to an object without requiring it to be loaded.
Detaching and Merging JPA Entity Objects
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.