Hibernate Lifecycle | States in Hibernate: Transient, Persistent, Detached, Removed

Nikhil Sukhani
4 min readMar 21, 2023

--

Hibernate (Source:GFG)

The Hibernate life cycle consists of four main states: Transient, Persistent, Detached, and Removed. Each of these states represents a specific state of an object in the Hibernate framework.

Transient State

When an object is created using the “new” keyword, it is in the transient state. The object is not associated with any Hibernate session, and no database operations are performed on it. The object is simply a plain Java object (POJO) that is not yet persisted in the database.

Code Example:

// Creating a new object in the transient state
Employee employee = new Employee();
employee.setName("John");
employee.setAge(30);

Persistent State

When an object is associated with a Hibernate session, it enters the persistent state. In this state, the object is associated with a specific Hibernate session and is actively managed by Hibernate. Any changes made to the object will be tracked by Hibernate and will be persisted to the database when the session is flushed.

There are two sub-states of the persistent state: Transient-Persistent and Persistent-Detached.

Transient-Persistent State

When an object is first associated with a Hibernate session, it is in the Transient-Persistent state. This means that the object is newly created, and its state is not yet synchronized with the database. Any changes made to the object in this state will be persisted to the database when the session is flushed.

Code Example:

// Creating a new object in the Transient-Persistent state
Employee employee = new Employee();
employee.setName("John");
employee.setAge(30);

// Associating the object with a Hibernate session
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
session.save(employee);

Persistent-Detached State

On the other hand, when an object is already in the database and is loaded into a Hibernate session, it is in the Persistent-Detached state. Any changes made to the object in this state will also be tracked by Hibernate and will be persisted to the database when the session is flushed.

Code Example:

// Loading an existing object into a Hibernate session
Session session = HibernateUtil.getSessionFactory().openSession();
Employee employee = session.get(Employee.class, 1L);

// Modifying the object in the Persistent-Detached state
employee.setName("Alice");

// Persisting the changes to the database
session.beginTransaction();
session.update(employee);

Detached State

When a persistent object is no longer associated with a Hibernate session, it enters the detached state. This means that the object is no longer actively managed by Hibernate, and any changes made to it will not be persisted to the database. However, the object is still a valid Java object and can be re-associated with a Hibernate session in the future.

Code Example:

// Loading an existing object into a Hibernate session
Session session = HibernateUtil.getSessionFactory().openSession();
Employee employee = session.get(Employee.class, 1L);

// Detaching the object from the Hibernate session
session.evict(employee);

// Modifying the object in the Detached state
employee.setAge(35);

// Re-associating the object with a Hibernate session
session.beginTransaction();
session.update(employee);

Removed State

When an object is deleted from the database, it enters the removed state. This means that the object is no longer associated with the database, and any attempts to modify it or re-associate it with a Hibernate session will result in an exception.

Code Example:

// Loading an existing object into a Hibernate session
Session session = HibernateUtil.getSessionFactory().openSession();
Employee employee = session.get(Employee.class, 1L);

// Deleting the object from the database and entering the Removed state
session.beginTransaction();
session.delete(employee);

Hibernate Life Cycle Transitions

Now that we have covered all the Hibernate life cycle states, let’s take a look at the transitions that occur between them.

Transient -> Persistent

The transient object becomes persistent when it is associated with a Hibernate session using the “save()” or “persist()” methods.

Code Example:

// Creating a new object in the transient state
Employee employee = new Employee();
employee.setName("John");
employee.setAge(30);

// Associating the object with a Hibernate session and entering the Persistent state
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
session.save(employee);

Persistent -> Detached

A persistent object becomes detached when the Hibernate session is closed, or the object is explicitly evicted from the session.

Code Example:

// Loading an existing object into a Hibernate session and entering the Persistent state
Session session = HibernateUtil.getSessionFactory().openSession();
Employee employee = session.get(Employee.class, 1L);

// Evicting the object from the session and entering the Detached state
session.evict(employee);

Detached -> Persistent

A detached object becomes persistent when it is re-associated with a Hibernate session using the “update()” or “merge()” methods.

Code Example:

// Loading an existing object into a Hibernate session and entering the Persistent state
Session session = HibernateUtil.getSessionFactory().openSession();
Employee employee = session.get(Employee.class, 1L);

// Evicting the object from the session and entering the Detached state
session.evict(employee);

// Modifying the object in the Detached state
employee.setAge(35);

// Re-associating the object with a Hibernate session and entering the Persistent state
session.beginTransaction();
session.update(employee);

Persistent -> Removed

A persistent object becomes removed when it is deleted from the database using the “delete()” method.

Code Example:

// Loading an existing object into a Hibernate session and entering the Persistent state
Session session = HibernateUtil.getSessionFactory().openSession();
Employee employee = session.get(Employee.class, 1L);

// Deleting the object from the database and entering the Removed state
session.beginTransaction();
session.delete(employee);

If this article helped you in any way, please react with a clap and help it reach a wider audience :)

Happy Learning!!

--

--

No responses yet