Oct 14, 2016

Object states in hibernate: Transient, Persistent and Detached

In hibernate, an object can have 3 states:
  • Transient 
  • Persistent 
  • Detached 

Transient Object

  • When an instance is created for an entity class using new() and has not been saved into the database is called Transient object. 
  • This object has no association with the Session object. 
  • Any modification done to this object does not make any reflections in the database since no row represents this object in the database. 

Persistent Object

  • When the instance of the entity class is saved in the database, it becomes Persistent object. 
  • A transient instance becomes persistent by associating it with the Session object. 
  • After an object becomes persistent, hibernate starts tracking the object for further modifications and automatically fires update statement for any changes made to the object. 
  • Hibernate will keep on tracking the object until it goes to Detached state. 

Detached Object

  • Once the Session object is closed, the persistent object becomes Detached object. 
  • This object now has no association with the Session object. 
  • Any modification done to this object now will not be reflected in the database. 
As can be seen in the below diagram, object can be moved to Persistent state by reattaching it back to session. It can be re-attached by updating it back to session. (Please note that it is possible to configure entity so that update is made only if there is some changes in the object state, by using @SelectBeforeUpdate annotation.)





Lets take an example to understand these various states. Following program assumes an Entity named "EmployeeDetails" having name and address properties.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.javaTerritory.hibernateDemo;
 
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
 
public class HibernateTestDemo {
 
      
       public static void main(String[] args) {
              EmployeeDetails employeeDetails = new EmployeeDetails();
              employeeDetails.setId(1111);
              employeeDetails.setUserName("Manoj");
              employeeDetails.setAddress("New Delhi");
              // Here 'employeeDetails' is TRANSIENT object
 
              SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
              Session session = sessionFactory.openSession();
              session.beginTransaction();
 
              session.save(employeeDetails); //After this statement, object gets associated with session and starts tracking it for further modification
             
              employeeDetails.setAddress("Mumbai");
              // Here 'employeeDetails' is PERSISTENT object, any update made to the object gets reflected in DB
 
              session.getTransaction().commit();
              session.close();
              // Here 'employeeDetails' is DETACHED object, any update made to the object does not get reflected in DB
              employeeDetails.setAddress("Kolkata");
} }

State of "employeeDetails" object is transient until line 22 gets executed.

After object is passed to hibernate to save in line number 22, it gets associated with the session and becomes persistent. In line number 26, address of object is changed from "New Delhi" to "Mumbai". Though object is not updated explicitly, hibernate automatically fires an update statement in database and updates the address.

After session is closed (line 31), object goes to the Detached state and hiibernate will not monitor any further modifications to the object. Thus line 35 will have no effect in the database.

Hopefully this explains the difference between the three states. It makes clear why transactions management is an important aspect in programming. It can lead to lazy loading exceptions if not managed properly.

Thanks for reading. If you liked the article, do like and follow us at +Java Territory to stay updated with latest updates.

0 comments:

Post a Comment