Hibernate Lifecycle (original) (raw)

Last Updated : 20 Apr, 2026

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 Lifecycle Diagram

Hibernate Lifecycle refers to the different states an entity object goes through from creation to deletion while interacting with the database in Hibernate. It describes how objects are managed within a session and how their changes are synchronized with the database.

Hibernate Lifecycle

Hibernate defines four primary states in an entity lifecycle:

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

State 1. Transient State

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.

State 2: Persistent State

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

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

`

State 3: Detached State

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

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:

State 4. Removed State

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

`