Sunday, January 3, 2016

Hibernate - State Changes of Object in Hibernate

Ref:- http://www.dineshonjava.com/p/understanding-state-changes-of-object.html#.VnIfgPkrLIU

  1. Transient State 
  2. Persistent State
  3. Detached State
Now you will learn how to flow of object in these states and how to manage by the hibernate.Look in the following figures-

1. When Creating a new Entity Object 

Here we see when create an object it is in transient state in this state hibernate does not ask for save this object means that in this state hibernate's session does not associate with that object in this state.
Once we calling session's save method now object move to the persistent state i.e. hibernate's session associated with that object in this state if any change made in the object hibernate ask to the database and made change in the database also.
After done our required events when we calling session's close method then object moves in the  detached  state.

2. When Reading an Entity Object
 


Here we see that we are not getting new object from new operator, we get the object from session's get method. Here we pass a primary key in the get method and getting a persistent object.

3. When Delete an Entity Object


Here after getting persistent object from session of hibernate. When we are calling the delete method of the session object moves from persistent state to the transient state. If we calling the close method of the session then that object moves to the detached state. If once object move to the transient state it never become a persistent object.

Now we look the states diagram of the entity object in the following.

 When object in the session area then it is in Persistent State.
 When object before the session area then it is in Transient State. 
 When object after the session area then it is in Detached State.

Detached to Persistent State:
 
Lets see in the following example how to an object moves from detached state to the persistent state again.
HibernateTestDemo.java
  1. package com.sdnext.hibernate.tutorial;  
  2.   
  3. import org.hibernate.Session;  
  4. import org.hibernate.SessionFactory;  
  5. import org.hibernate.cfg.AnnotationConfiguration;  
  6.   
  7. import com.sdnext.hibernate.tutorial.dto.UserDetails;  
  8.   
  9. public class HibernateTestDemo {  
  10.   
  11.  /** 
  12.   * @param args 
  13.   */  
  14.  public static void main(String[] args)   
  15.  {  
  16.   UserDetails userDetails = new UserDetails();  
  17.   //Here 'userDetails' is in TRANSIENT state  
  18.     
  19.   SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();  
  20.   Session session = sessionFactory.openSession();  
  21.   session.beginTransaction();  
  22.     
  23.   userDetails = (UserDetails) session.get(UserDetails.class1);  
  24.   //Here 'userDetails' is in PERSISTENT state  
  25.     
  26.   session.save(userDetails);  
  27.   session.getTransaction().commit();  
  28.   session.close();  
  29.     
  30.   session = sessionFactory.openSession();  
  31.   session.beginTransaction();  
  32.     
  33.   userDetails.setUserName("User Updated after session close");  
  34.   //Here 'userDetails' is in DETACHED state  
  35.     
  36.   session.update(userDetails);  
  37.   //Here 'userDetails' is again in PERSISTENT state  
  38.   session.getTransaction().commit();  
  39.   session.close();  
  40.  }  
  41. }  


Output:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).
log4j:WARN Please initialize the log4j system properly.
Hibernate: select userdetail0_.USER_ID as USER1_0_0_, userdetail0_.ADDRESS as ADDRESS0_0_, userdetail0_.USER_NAME as USER3_0_0_ from User_Details userdetail0_ where userdetail0_.USER_ID=?
Hibernate: select userdetail_.USER_ID, userdetail_.ADDRESS as ADDRESS0_, userdetail_.USER_NAME as USER3_0_ from User_Details userdetail_ where userdetail_.USER_ID=?
Hibernate: update User_Details set ADDRESS=?, USER_NAME=? where USER_ID=?


No comments:

Post a Comment