Hibernate - Entity Lifecyle



An entity in Hibernate is an object that is mapped to a database table. An entity can be in one of the four states −

  • Transient − Not persistent. Just instantiated, or maybe some methods have been called, but not associated with a Session.

  • Persistent − Associated with a Session. As soon as we call save or persist, the object is in persistent state.

  • Detached − Previously attached to a Session, but not anymore.

  • Removed − An entity enters the removed state when it is marked for deletion from the database.

Let's discuss each state in details.

Transient state

Transient state means an entity is not persistent. It is just instantiated, or maybe some methods have been called, but not associated with a Session. Objects can enter persistent state by calling save, persist or saveOrUpdate() methods.

Example

Employee employee = new Employee();
employee.setName("John Doe");
employee.setSalary(50000.0);

In the above example, the Employee object is in the transient state because it is not yet associated with a Hibernate session.

Persistent state

Persistent state refers to an association with a session. As soon as we call save or persist, the object is in persistent state. Persistent objects can become transient when the delete() method is called. Calling get() and load() on Session, returns a persistent entity.

Example

Session session = sessionFactory.openSession();
session.beginTransaction();

Employee employee = new Employee();
employee.setName("John Doe");
employee.setSalary(50000.0);

session.save(employee); // The entity is now in the persistent //state

session.getTransaction().commit();
session.close();

Here, the Employee object becomes persistent when it is saved using the session.save() method. Any changes made to the employee object will be reflected in the database.

Detached state

Detached state refers to state where an entity was previously attached with a session, but not anymore. An entity enters the detached state when the Hibernate session that was managing it is closed. In this state, the entity is no longer associated with any session, and changes made to it are not automatically synchronized with the database.

Example

Session session = sessionFactory.openSession();
session.beginTransaction();

Employee employee = session.get(Employee.class, 1L);
session.getTransaction().commit();
session.close(); // The entity is now in the detached state

employee.setSalary(60000.0); // Changes are not synchronized with the database

Removed state

An entity enters the removed state when it is marked for deletion from the database. In this state, the entity is still associated with a session, but it will be deleted from the database when the transaction is committed.

Example

Session session = sessionFactory.openSession();
session.beginTransaction();

Employee employee = session.get(Employee.class, 1L);
session.delete(employee); // The entity is now in the removed state

session.getTransaction().commit();
session.close();

Transitioning Between States

Entities can transition between these states through various Hibernate methods −

  • Transient to Persistent − Calling session.save() or session.persist() method, moves the entity from transient to persistent state.

  • Persistent to Detached − Calling session.evit() or session.clear() or session.close() method, cause the entity to be in detached state.

  • Detached to Persistent − Call session.update() or session.merge() method to persist an detached entity.

  • Persistent to Removed − Remove an entity by calling session.delete() method.

Advertisements