JPA Entity lifecycle – JPA developer should know


In this tutorial we are going to see about JPA Entity lifecycle and Instance state transitions.

1. Entity Instance states

JPA defines four states and state transitions for the persistence life cycle. 

  1. Transient 
  2. Managed or Persistent 
  3. Removed 
  4. Detached 

Following image illustrates the overview of JPA entity lifecycle state transitions. 

2. Transient State :

  • When an entity object is initially created, it’s state is New or Transient. In this state, the object is not yet associated with an EntityManager and has no representation in the database.
  • Objects instantiated using the new operator are not immediately persistent (Managed). Their state is “Transient”. Which means they are not associated with any database table row, and so their state is lost as soon as they are reference removed. 
  • Transient state instances state is lost and garbage-collected as soon as they are no longer referenced. 

3. Persistent/Managed State :

  • An entity object becomes Managed or Persistent when it is persisted to the database via an EntityManager’s persist() method, which must be invoked within an active transaction. 
  • Persistent state instances are always associated with persistence-context and transnational. 
  • If we are changing persistent State object values those are synchronized automatically with the database while committing the transaction. 
  • Entity objects retrieved from the database by an EntityManager are also in the Managed state. 

4. Detached State :

  • The state, Detached, represents entity objects that have been disconnected from the EntityManager. 
  • Object identifier should be there in database table and Object should not associated with persistence-context.
  • Instances are lose their association with persistence manager when you close() the hibernate session. We refer to these objects as detached. 
  • Indicating that their state is no longer guaranteed to be synchronized with database, they are no longer under the management if their references removed, then they are eligible for garbage collector. 

5. Removed State :

  • A persistent state entity object can also be marked for deletion, by using the EntityManger’s remove() with in an active transaction. Then entity object changes its state from Managed to Removed, and physically deleted from the database during commit. 

6. What is Persistence Context? 

  • The persistence context is the collection of all persistent (managed) objects of an EntityManager. 
  • If we try to retrieve an entity, if the entity is existed in persistence context, then managed entity object returned from persistence context without accessing the database. 
  • Note that, refresh() always requires access database. 
  • The main role of the persistence context is to make sure that a database entity object is represented by no more than one in-memory entity object within the same EntityManager. 
  • Every EntityManager manages it’s own persistence context. 
  • Persistence context act as a local cache for a given EntityManager. 

To check if entity object exist in persistence context, we can use EntityManager’s contains() method. And clear() method will be used to clear persistence context. 

boolean isManaged = entityManager.contains(student); 

7. Conclusion

In this tutorial we have learned about JPA entity lifecycle and instance state transitions.

8. References

  1. ObjectDB JPA Entity Objects
  2. OpenJpa



Please enter your comment!
Please enter your name here