In the previous tutorial you learned about the three states of the entity object using by the Hibernate API. An object can have three states: transient, persistent and detached. Object before association session it is in Transient. When associated session it is in Persistent. Detaching from session it is in  Detached.
  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

State of Objects in Hibernate
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
 

State of Objects

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

State of Objects Hibernate

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.

Spring 5 Design Pattern Book

You could purchase my Spring 5 book that is with title name “Spring 5 Design Pattern“. This book is available on the Amazon and Packt publisher website. Learn various design patterns and best practices in Spring 5 and use them to solve common design problems. You could use author discount to purchase this book by using code- “AUTHDIS40“.
Spring-5-Design-Pattern

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

Object States in Hibernate

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:

Detached to Persistent State in Hibernate

 

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:

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=?

detached to persistent

 

In the Next Chapter we will discuss about the Hibernate Query Language(HQL).

Hibernate Tutorial Contents

  1. Introduction to Hibernate 3.0
  2. Hibernate Architecture 
  3. Setting Up Hibernate
  4. Writing a First Hibernate Application Using Annotation
  5. Writing a First Hibernate Application Using Mapping File
  6. Understanding Hibernate O/R Mapping 
  7. Saving Objects using Hibernate APIs
  8. Using Annotations vs Configuration files
  9. hbm2ddl Configuration and Name Annotations
  10. Retrieving Objects using session.get
  11. Hibernate Update Query
  12. Hibernate Delete Query
  13. Value Types and Embedding Objects
  14. Saving Collections
  15. Configuring Collections and Adding Keys
  16. Proxy Objects and Eager and Lazy Fetch Types
  17. Hibernate One-To-One Mapping Tutorial   
  18. Hibernate One-To-Many Mapping Tutorial 
  19. Hibernate Many-To-One Mapping Tutorial 
  20. Hibernate Many-To-Many Mapping Tutorial 
  21.  CascadeTypes and Other Things
  22.   Implementing Inheritance in Hibernate (Single Table Strategy, With Table Per Class Strategy, With Joined Strategy)
  23. CRUD Operations Using Hibernate 3 (Annotation and Configuration)
  24. Transient, Persistent and Detached Objects in Hibernate
  25. Understanding State Changes of Object in Hibernate
  26.  Introducing HQL(Hibernate Query Language) and the Query Object
  27. Select and Pagination in HQL
  28. Understanding Parameter Binding and SQL Injection
  29. Named Queries
  30. Introduction to Criteria API
  31. Understanding Restrictions
  32. Cacheing in Hibernate: First Level and Second Level Cache in Hibernate
  33. Using Query Cache in Hibernate
  34. Hibernate Batch Processing


                <<Previous Chapter 24<<    >>Next Chapter 26>>