Life cycle of objects in Hibernate

Connect with

Life cycle of objects in HibernateThere are three states or Life cycle of objects in Hibernate which are: Transient, Persistent and Detached. Learn Hibernate Entity life cycle in this post.

1. Overview of Hibernate Entity Life cycle

In this article you learn what is Hinernate entity life cycle or you can say different states of object/entity in Hibernate. Knowing of persistent entity life cycle in Hibernate is essential for any developer or architect. There are three states of any object in Hibernate as follow :

  1. Transient state
  2. Persistent state
  3. Detached state

In any application with a persistent state, the application must interact with the persistence layer i.e. DAO: Data Access Object or you can call it DAL (Data Access Layer). In this post , I explain these three persistent states: Transient, Persistent, and Detached.

2. Overview of different methods for persistent state

This graph has been taken to describe about how the state of the object goes from one persistent state to another by calling a particular method. You can find a high-level overview by going through the method of the Hibernate session.

Object life cycle in Hibrenate ORM

The above graph describes how an object goes from one persistent state to another by calling of the method on Hibernate session or Persistence manager.

3. Transient objects: Entity Lifecycle

In Hibernate, objects instantiated using the new operator (e.g. new Employee("ranjeet", 2); ) aren’t immediately goes into persistent state. Transient state means objects aren’t associated with any database table or row. When an object associated with the Hibernate session then it goes to a Persistent state, which means before an association of Hibernate’s session. secondly, when it detached from Hibernate Session, it goes again to the Transient state.

In Hibernate, all transient instances to be nontransactional; a modification to the state of a transient instance isn’t made in the context of any transaction. This means Hibernate doesn’t provide any rollback functionality for transient objects. For an instance to transition from transient to persistent state requires either a save() call to the persistence manager or the creation of a reference from an already persistent instance.

 public int saveEmployee(Employee employee) {
    Session session = HibernateUtil.getSessionFactory().openSession();
    Transaction transaction = null;
    int empId = 0;
    try {
      transaction = session.beginTransaction();
      empId = (Integer) session.save(employee);
      transaction.commit();
    } catch (HibernateException e) {
      transaction.rollback();
      e.printStackTrace();
    } finally {
      session.close();
    }
    return empId;
  }
Note: when line no 7 executes, you can say the object in a persistent state, and when this method returns it goes in a detached state.

Persistent objects: Hibernate entity life cycle

When it attached to the Hibernate session means the object is in a persistent state. Persistent instances are then associated with the persistence manager. Persistent instances participate in transactions—their state is synchronized with the database at the end of the transaction. When a transaction commits, state held in-memory is propagated to the database by the execution of SQL INSERT, UPDATE, and DELETE statements.

Detached objects : Hibernate entity life cycle

When a transaction completes, the persistent instances associated with the persistence manager still exist. If the transaction were successful, their in-memory state will have been synchronized with the database. In the case of Hibernate, however, these instances lose their association with the persistence manager when you close() the Session. We refer to these objects as detached, meaning that their state is no longer guaranteed to be synchronized with the database state; they’re no longer under the session of Hibernate. You can say when the method returns an entity associated with the session, will have to go in a detached state.

7. Reference and Source Code

Thanks for visiting this post for Hibernate entity life cycle. You can also visit Java Hibernate ORM Tutorial Listing page for more articles on Hibernate ORM framework.
Happy Learning 🙂 for for Hibernate object life cycle.
Your comments are welcome to improve this post of Hibernate object state life cycle. I hope you enjoyed by reading this article.


Connect with

Leave a Comment

Your email address will not be published. Required fields are marked *