☰ See All Chapters |
JPA Entity Lifecycle
In JPA we can find 3 states in the lifecycle of entity object, they are:
Transient state of an object
Persistent state of an object or Managed state of an object
Detached state of an object.
EntityManagerFactory emf = Persistence.createEntityManagerFactory("StudentPU"); EntityManager em = emf.createEntityManager(); em.getTransaction().begin(); Student student = new Student(); //now student is transient object em.persist(student); //now student is managed object em.getTransaction().commit(); //now student is detached
Student stdObj = em.find(Student.class, 1); em.getUserTransaction().begin(); em.remove(stdObj); // now stdObj object is removed, but not detached. em.getUserTransaction().commit(); // now stdObj object is detached. |
1. Transient state of an object
Object which is not participated in any JPA session operations is called as transient object. The state of this object is called as transient state. When object is in transient state it does not contain any identity (primary key value) and it does not represents any record in Database table.
2. Persistent /Managed state of an object
Object which is participated in JPS session operations is called persistent object. The state of this object is called persistent state. When object is in persistent state it contains an identity. It represents any record in Database table and maintains synchronization with that record. The environment in which Persistent objects persists is called persistence context.
3. Detached state of an object
Object which is just removed from JPA session is called detached object and the state is called as detached state. When object is in detached state it contains identity but you cannot do persistent operations with that identity. It contains identity value but does not represent any record in a table. Detached objects have represented a record earlier, but not representing the record currently, that is the reason to have identity in detached state.
The below table explains how objects behave in different states when persisted, removed, refreshed and merged:
| Persist | Remove | Refresh | Merge |
New entity | It becomes managed | It is ignored. | It is ignored. | Its state is copied into existing managed instance of the same entity identity, or a new managed copy of object is created. |
Existing managed entity | It is ignored. | It becomes removed. | Its state is refreshed from the database. | It is ignored. |
Removed entity | It becomes managed | It is ignored. | It is ignored. | IllegalArgumentException is thrown. |
Detached entity | IllegalArgumentExcepton is thrown. | IllegalArgumentException is thrown. | IllegalArgumentException is thrown. | Its state is copied into existing managed instance of the same entity identity, or a new managed copy of object is created. |
The above three listed states of entity are actual states of entity objects. There is another term called removed object, as it denotes, this is the state of the entity relative to the database - though it is not a "real" state but a programmed state, it will synchronize with database on next commit/flush. When an entity is removed, it will be called as removed object till commit/flush. Once committed /flushed to database, it becomes detached object.
All Chapters