Hibernate Entity LifeCycle

We know that Hibernate works with plain old Java objects (POJO). In raw form (without hibernate-specific annotations), it will not be able to identify these POJO classes.

But when the POJOs are properly annotated with required annotations then hibernate will be able to identify and work with them e.g. store them in the database, update them, etc. These POJOs are said to be managed by hibernate’s persistent context or hibernate entities.

A picture speaks a thousand words. Let us understand how the transition happens between the various lifecycle states of an entity.

1. Entity Lifecycle States

Given an instance of a class that is managed by persistent context, it can be in any one of four different persistence states (known as hibernate entity lifecycle states):

  1. Transient
  2. Persistent or Managed
  3. Detached
  4. Removed

1.1. Transient

Transient entities exist in heap memory as normal Java objects. Hibernate does not manage transient entities. The persistent context does not track the changes done on them.

  • In simple words, a transient entity has neither any representation in the datastore nor in the current Session.
  • A transient entity is simply a POJO without any identifier.
Transient objects are independent of Hibernate
EmployeeEntity employee = new EmployeeEntity();

1.2. Persistent or Managed

Persistent entities exist in the database, and Hibernate’s persistent context tracks all the changes done on the persistent entities by the client code.

A persistent entity is mapped to a specific database row, identified by the ID field. Hibernate’s current running Session is responsible for tracking all changes done to a managed entity and propagating these changes to database.

Persistent objects are maintained by Hibernate
Persistent objects are maintained by Hibernate

We can get persistent entity in either of two ways:

  • Load the entity using get() or load() method.
  • Persist the transient or detached entity using persist(), save(), update() or saveOrUpdate() methods.
EmployeeEntity employee = session.load(1);

//or

EmployeeEntity employee = new EmployeeEntity();
session.save(employee);

1.3. Detached

Detached entities have a representation in the database but these are currently not managed by the Session. Any changes to a detached entity will not be reflected in the database, and vice-versa.

A detached entity can be created by closing the session that it was associated with, or by evicting it from the session with a call to the session’s evict() method.

session.close();

//or
session.evict(entity);

This temporary separation of the entity and the database is shown in the image below.

Detached objects exist in the database but are not maintained by Hibernate
Detached objects exist in the database but are not maintained by Hibernate

One reason you might consider doing this would be to read an object out of the database, modify the properties of the object in memory, and then store the results some place other than your database. This would be an alternative to doing a deep copy of the object.

Note that in order to make a persistent entity from a detached entity, the application must re-attach it to a valid Hibernate Session. A detached instance can be associated with a new Hibernate session when your application calls one of the load(), refresh(), merge(), update(), or save() methods on the new session with a reference to the detached object.

After the method call, the detached entity would be a persistent entity managed by the new Hibernate session.

1.4. Removed

Removed entities are objects that were being managed by Hibernate (persistent entities, in other words) and now those have been passed to the session’s remove() method.

When the application marks the changes held in the Session as to be committed, the entries in the database that correspond to removed entities are deleted.

session.remove(employee);

2. Conclusion

  1. The newly created POJO object will be in the transient state. The transient entity doesn’t represent any row of the database i.e. not associated with any session object. It’s a plain simple java object.
  2. A persistent entity represents one row of the database and is always associated with some unique hibernate session. Changes to persistent objects are tracked by hibernate and are saved into the database when commit calls happen.
  3. Detached entities are those who were once persistent in the past, and now they are no longer persistent. To persist changes done in detached objects, you must re-attach them to hibernate session.
  4. Removed entities are persistent objects that have been passed to the session’s remove() method and soon will be deleted as soon as changes held in the session will be committed to the database.

Happy Learning !!

Comments

Subscribe
Notify of
guest
4 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial

Dark Mode

Dark Mode