Open In App

Hibernate Lifecycle

Last Updated : 30 Oct, 2025
Comments
Improve
Suggest changes
18 Likes
Like
Report

In Hibernate, every entity object passes through a set of well-defined states from creation to deletion. This progression is known as the Hibernate Object Lifecycle. Understanding these states is essential to manage persistence operations effectively.

Hibernate defines four primary states in an entity lifecycle:

  1. Transient State
  2. Persistent State
  3. Detached State
  4. Removed State

Hibernate Lifecycle Diagram

Hibernate Lifecycle

1. Transient State

  • When an entity object is created using the new operator, it starts in the transient state.
  • It is not associated with any Hibernate session and not stored in the database.
  • Any modification to this object does not affect the database since Hibernate does not track it yet.
Changing new object to Transient State

Example:

Java
Employee e = new Employee();
e.setId(21);
e.setFirstName("Neha");
e.setMiddleName("Shri");
e.setLastName("Rudra");

Here, the Employee object exists only in heap memory and has no database connection.

Key Points:

  • Created using new.
  • Not associated with Hibernate Session.
  • No corresponding database row.

State 2: Persistent State

  • When a transient object becomes associated with a Hibernate session, it moves into the persistent state.
  • Hibernate starts tracking changes to the object. Any modification to fields automatically updates the corresponding database record during transaction commit.

Transition from Transient → Persistent: Occurs when you call any of these methods:

  • session.save(e)
  • session.persist(e)
  • session.saveOrUpdate(e)
  • session.update(e)
  • session.merge(e)
  • session.lock(e)
Converting Transient State to Persistent State

Example:

Java
Employee e = new Employee("Neha Shri Rudra", 21, 180103);
session.save(e); // The object is now persistent
  • Associated with an open Hibernate Session.
  • Represents a database row.
  • Changes are synchronized with the database.

State 3: Detached State

  • When a persistent object’s session is closed or cleared, it enters the detached state.
  • The object still exists in memory but is no longer managed by Hibernate.
  • Any modification made to it will not be reflected in the database unless reattached.

Transition from Persistent → Detached: Occurs when you call any of these methods:

  • session.close()
  • session.clear()
  • session.detach(e)
  • session.evict(e)
Converting Persistent State to Detached State

Example:

Java
Employee e = new Employee("Neha Shri Rudra", 21, 180103);
session.save(e);
session.close(); // The object is now detached

To reattach a detached object:

  • session.update(e)
  • session.merge(e)
  • session.refresh(e)
  • session.load(e)

Key Points:

  • Session closed or cleared.
  • No synchronization with the database.
  • Can be reattached later.

4. Removed State

  • The removed state represents a deleted entity.
  • When a persistent object is deleted using session.delete(), it is removed from both the session and the database.
  • Any further changes to the object will not affect the database.
Converting Persistent State to Removed State

Example:

Java
Employee e = new Employee();
Session s = sessionFactory.openSession();
s.save(e);
s.delete(e); // The object is now removed
  • Object is deleted from the database.
  • No longer tracked by Hibernate.
  • Further changes have no effect.

Article Tags :

Explore