Hibernate Lifecycle

Hibernate Lifecycle

In Hibernate, the mapped instances of the entity classes have a lifecycle. Either we can create a new object, or we can fetch the existing data from the database. The value objects do not have a lifecycle, only the entity object do. Hibernate prefers its object to be in a particular “state,” and there are mainly four states of the object.

Following are the states of the object's lifecycle:

  • Transient State
  • Persistent State
  • Detached State
  • Removed State
Hibernate Lifecycle

Transient State

An object is said to be transient, if it is initialized using the new operator, and it is not associated with any Hibernate Session. As it is not associated with any Session, it doesn’t have any representation in the database table. If we made changes to the data of the POJO class, it would not affect the database.

For example:

There exists a POJO class called Payment.java. We are creating a new object for the Payment class. Following code shows the initialization of the Payment object:

Payment pay = new Payment();     //The object pay is in transient state

To make an object persistent, we use Hibernate Session. A transient object may be made persistent by calling save(), persist() or saveOrUpdate() functions.

Persistent State

A persistent object has a representation in the database, an identifier value, and it is also associated with a Hibernate Session. In this state, Hibernate will detect changes made to an object and synchronize it with the database. If we made changes in the data, it would also affect the database. It is also present in a heap.

We can create a persistent object by calling session.save(), session.persist(), session.update(), session.saveOrUpdate(), session.lock() and session.merge().

Following code shows the transformation of a transient object into persistent.

Payment pay = new Payment();     //The object pay is in transient state
Session s = sessionfactory.openSession();
pay.setId(01);
session.save(pay)                   //The object pay  is in persistent state. 

A persistent object may be made transient by calling session.delete() function.

Detached State

When the session of a persistent object has been closed, the object will be known as a detached object. As a detached object is no more associated with the Session, changes made to the data will not affect the database. The detached object can be reattached to a new session when needed.

We can create a detached object by calling session.close(), session.clear(), session.detach() and session.evict().

Following code shows the initialization of detached object:

Payment pay = new Payment();     //The object pay is in transient state
 Session s = sessionfactory.openSession();
 pay.setId(01);
 session.save(pay)                   //The object pay  is in persistent state.
 session.close(pay):               //The object pay is in detached state. 

+

Removed State

An object is said to be in the removed state when it is deleted from the database. In this state, the object may still exist, but Hibernate will ignore it. Any changes made to the data will not affect the database.

 We can create a removed object by calling session.delete().

Following code shows the initialization of removed object:

Payment pay = new Payment();     //The object pay is in transient state
Session s = sessionfactory.openSession();
pay.setId(01);
session.save(pay)                   //The object pay  is in persistent state. 
session.delete(pay);             //The object pay is in removed state.