Popular Tutorials
After done our required events when we calling session’s close method then object moves in the detached state.
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.
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.
Lets see in the following example how to an object moves from detached state to the persistent state again.
HibernateTestDemo.java
package com.sdnext.hibernate.tutorial; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.AnnotationConfiguration; import com.sdnext.hibernate.tutorial.dto.UserDetails; public class HibernateTestDemo { /** * @param args */ public static void main(String[] args) { UserDetails userDetails = new UserDetails(); //Here 'userDetails' is in TRANSIENT state SessionFactory sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory(); Session session = sessionFactory.openSession(); session.beginTransaction(); userDetails = (UserDetails) session.get(UserDetails.class, 1); //Here 'userDetails' is in PERSISTENT state session.save(userDetails); session.getTransaction().commit(); session.close(); session = sessionFactory.openSession(); session.beginTransaction(); userDetails.setUserName("User Updated after session close"); //Here 'userDetails' is in DETACHED state session.update(userDetails); //Here 'userDetails' is again in PERSISTENT state session.getTransaction().commit(); session.close(); } }
Output:
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=?
In the Next Chapter we will discuss about the Hibernate Query Language(HQL).
Hibernate Tutorial Contents
<<Previous Chapter 24<< >>Next Chapter 26>>
View Comments
Hi Dinesh, You used some image to show the examples of entity states but those image are not visible. Please see your sub headings that are
2. When Reading an Entity Object
3. When Delete an Entity Object
Detached to Persistent State:
Sorry for inconvenience, now issues have been resolved please check.