<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div style="color: #20124d;">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.</div>
<div id="ads-id" align="center"></div>
<ol style="color: #20124d; text-align: left;">
<li>Transient State</li>
<li>Persistent State</li>
<li>Detached State</li>
</ol>
<div style="color: #20124d;">Now you will learn how to flow of object in these states and how to manage by the hibernate.Look in the following figures-</div>
<div style="color: #20124d;"></div>
<h2><b>1. When Creating a new Entity Object </b></h2>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2017/04/three_states.jpg" width="640" height="334" border="0" /></div>
</div>
<div style="background-color: #f2f9fc; border: 1px solid #c9e6f2; border-radius: 3px; padding: 16px; line-height: 1.45;">
<p><span style="color: red; font-size: x-large; text-align: center;"><b>Popular Tutorials</b></span></p>
<ul style="text-align: left;">
<li><b><a href="https://dineshonjava.com/spring-tutorial/"><em><strong>Spring Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-web-mvc-framework-chapter-38/"><strong><em>Spring MVC Web Tutorial </em></strong></a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-spring-boot-a-spring-boot-complete-guide/"><strong>Spring Boot Tutorial</strong> </a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-take-baby-step-to-secure/"><em>Spring Security Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint/"><em>Spring AOP Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/using-spring-jdbc-framework-chapter-32/"><em>Spring JDBC Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-hateoas-hypermedia-driven-restful-web-service/"><em><strong>Spring HATEOAS </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/microservices-with-spring-boot/"><em><strong>Microservices with Spring Boot</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/jax-rs-web-service-tutorial/"><strong><em>REST Webservice</em> </strong></a></b></li>
<li><b><a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/"><em><strong>Core Java </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/hibernate-3-on-baby-steps/"><em><strong>Hibernate Tutorial</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-batch-process-with-example/"><strong><em>Spring Batch</em> </strong></a></b></li>
</ul>
</div>
<div style="color: #20124d;">Here we see when create an object it is in <b>transient </b>state in this state hibernate does not ask for save this object means that in this state hibernate&#8217;s session does not associate with that object in this state.</div>
<div style="color: #20124d;">Once we calling session&#8217;s save method now object move to the <b>persistent </b>state i.e. hibernate&#8217;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.</div>
<div style="color: #20124d;">
<p>After done our required events when we calling session&#8217;s close method then object moves in the <b>detached </b>state.</p>
<h2><b>2. When Reading an Entity Object</b><br />
<b> </b></h2>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2017/04/ReadState.jpg" border="0" /></div>
<p>Here we see that we are not getting new object from <b>new op</b>erator, we get the object from session&#8217;s get method. Here we pass a primary key in the get method and getting a persistent object.</p>
<h2><b>3. When Delete an Entity Object</b></h2>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2017/04/deleteState.jpg" width="640" height="190" border="0" /></div>
<p>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.</p>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px 16px 16px 16px;">
<h2 dir="ltr">Spring 5 Design Pattern Book</h2>
<div dir="ltr" style="color: #20124d; font-family: 'calibri' , sans-serif; font-size: 13pt; text-align: justify;">You could purchase my <strong>Spring 5 book</strong> that is with title name &#8220;<strong>Spring 5 Design Pattern</strong>&#8220;. This book is available on the <a href="https://www.amazon.in/Spring-Design-Patterns-Dinesh-Rajput/dp/1788299450/ref=sr_1_1?ie=UTF8&;qid=1507925340&;sr=8-1&;keywords=spring+5+design+pattern" target="_blank" rel="noopener"><strong>Amazon</strong></a> and <a href="https://www.packtpub.com/application-development/spring-5-design-patterns" target="_blank" rel="noopener"><strong>Packt</strong></a> publisher website. Learn various <strong>design patterns</strong> and <strong>best practices</strong> in Spring 5 and use them to solve common design problems. You could use author discount to purchase this book by using code- &#8220;<strong>AUTHDIS40</strong>&#8220;.</div>
<div dir="ltr"></div>
<div dir="ltr"><a href="https://www.amazon.in/Spring-Design-Patterns-Dinesh-Rajput/dp/1788299450/ref=sr_1_1?s=books&;ie=UTF8&;qid=1512607966&;sr=1-1&;keywords=dinesh+rajput" target="_blank" rel="noopener"><img class="aligncenter wp-image-3053 size-medium" title="Spring-5-Design-Pattern" src="https://dineshonjava.com/wp-content/uploads/2013/03/Spring-5-design-pattern-243x300.jpg" width="243" height="300" /></a></div>
</div>
<p>Now we look the states diagram of the entity object in the following.</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2017/04/objectState.jpg" width="640" height="202" border="0" /></div>
<p>When object in the session area then it is in <b>Persistent State.</b><br />
<b> </b>When object before the session area then it is in <b>Transient State. </b><br />
<b> </b>When<b> </b>object after the session area then it is in <b>Detached State.</b></p>
<h2><b>Detached to Persistent State:</b></h2>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2017/04/dtp.jpg" border="0" /></div>
<p><b> </b><b> </b></p>
</div>
</div>
<p>Lets see in the following example how to an object moves from detached state to the persistent state again.<br />
<b>HibernateTestDemo.java</b></p>
<pre class="highlight">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(); 
 } 
} 
</pre>
<div style="background-color: #ff99cc; border-width: thin; border: solid;">
<p><b>Output:</b></p>
<div style="color: red;">log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version).</div>
<div style="color: red;">log4j:WARN Please initialize the log4j system properly.</div>
<p>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=?<br />
Hibernate: select userdetail_.USER_ID, userdetail_.ADDRESS as ADDRESS0_, userdetail_.USER_NAME as USER3_0_ from User_Details userdetail_ where userdetail_.USER_ID=?<br />
Hibernate: update User_Details set ADDRESS=?, USER_NAME=? where USER_ID=?</p>
</div>
<div class="separator" style="clear: both; text-align: center;"><img class="aligncenter" src="https://dineshonjava.com/wp-content/uploads/2017/04/d2p-1024x165.jpg" width="640" height="103" border="0" /></div>
<p> ;</p>
</div>
<p><b>In the <a href="https://dineshonjava.com/p/introducing-hqlhibernate-query-language.html">Next Chapter</a> we will discuss about the <a href="https://dineshonjava.com/p/introducing-hqlhibernate-query-language.html">Hibernate Query Language(HQL)</a>.</b></p>
</div>
</div>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px;">
<p><span style="color: red; font-size: x-large; text-align: center;"><b>Hibernate Tutorial Contents</b></span></p>
<ol style="text-align: left;">
<li><a href="https://dineshonjava.com/hibernate-3-on-baby-steps/"><b>Introduction to Hibernate 3.0</b></a></li>
<li><a href="https://dineshonjava.com/hibernate-architecture/"><b>Hibernate Architecture</b></a><b> </b></li>
<li><a href="https://dineshonjava.com/chapter-3-setting-up-hibernate-to/"><b>Setting Up Hibernate</b></a></li>
<li><a href="https://dineshonjava.com/chapter-4-writing-first-hibernate/"><b>Writing a First Hibernate Application Using Annotation</b></a></li>
<li><a href="https://dineshonjava.com/chapter-5-writing-first-hibernate/"><b>Writing a First Hibernate Application Using Mapping File </b></a></li>
<li><a href="https://dineshonjava.com/chapter-6-understanding-hibernate-or/"><b>Understanding Hibernate O/R Mapping</b></a><b> </b></li>
<li><a href="https://dineshonjava.com/saving-objects-using-hibernate-apis/"><b>Saving Objects using Hibernate APIs</b></a></li>
<li><a href="https://dineshonjava.com/using-annotations-vs-configuration/"><b>Using Annotations vs Configuration files</b></a></li>
<li><a href="https://dineshonjava.com/hbm2ddl-configuration-and-name/"><b>hbm2ddl Configuration and Name Annotations</b></a></li>
<li><a href="https://dineshonjava.com/retrieving-objects-using-sessionget/"><b>Retrieving Objects using session.get</b></a></li>
<li><b><a href="https://dineshonjava.com/hibernate-update-query/">Hibernate Update Query</a></b></li>
<li><b><a href="https://dineshonjava.com/hibernate-delete-query/">Hibernate Delete Query</a></b></li>
<li><a href="https://dineshonjava.com/value-types-and-embedding-objects/"><b>Value Types and Embedding Objects</b></a></li>
<li><a href="https://dineshonjava.com/saving-collections/"><b>Saving Collections</b></a></li>
<li><a href="https://dineshonjava.com/configuring-collections-and-adding-keys/"><b>Configuring Collections and Adding Keys</b></a></li>
<li><a href="https://dineshonjava.com/proxy-objects-and-eager-and-lazy-fetch/"><b>Proxy Objects and Eager and Lazy Fetch Types</b></a></li>
<li><b><a href="https://dineshonjava.com/hibernate-one-to-one-mapping-tutorial/">Hibernate One-To-One Mapping Tutorial</a></b><b> </b><b> </b><b> </b></li>
<li><b><a href="https://dineshonjava.com/hibernate-one-to-many-mapping-tutorial/">Hibernate One-To-Many Mapping Tutorial</a></b><b> </b></li>
<li><b><a href="https://dineshonjava.com/hibernate-many-to-one-mapping-tutorial/">Hibernate Many-To-One Mapping Tutorial</a></b><b> </b></li>
<li><b><a href="https://dineshonjava.com/hibernate-many-to-many-mapping-tutorial/">Hibernate Many-To-Many Mapping Tutorial</a></b><b> </b></li>
<li><b> <a href="https://dineshonjava.com/cascadetypes-and-other-things/">CascadeTypes and Other Things</a></b></li>
<li><b><a href="https://dineshonjava.com/implementing-inheritance-in-hibernate/"> Implementing Inheritance in Hibernate (Single Table Strategy, With Table Per Class Strategy, With Joined Strategy)</a></b></li>
<li><b><a href="https://dineshonjava.com/crud-operations-using-hibernate-3/">CRUD Operations Using Hibernate 3 (Annotation and Configuration)</a></b></li>
<li><b><a href="https://dineshonjava.com/transient-persistent-and-detached/">Transient, Persistent and Detached Objects in Hibernate</a></b></li>
<li><b><a href="https://dineshonjava.com/understanding-state-changes-of-object/">Understanding State Changes of Object in Hibernate</a></b></li>
<li><b><a href="https://dineshonjava.com/introducing-hqlhibernate-query-language/"> Introducing HQL(Hibernate Query Language) and the Query Object</a></b></li>
<li><b><a href="https://dineshonjava.com/select-and-pagination-in-hql/">Select and Pagination in HQL</a></b></li>
<li><b><a href="https://dineshonjava.com/understanding-parameter-binding-and-sql/">Understanding Parameter Binding and SQL Injection</a></b></li>
<li><b><a href="https://dineshonjava.com/named-queries/">Named Queries</a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-criteria-api/">Introduction to Criteria API</a></b></li>
<li><b><a href="https://dineshonjava.com/understanding-restrictions/">Understanding Restrictions</a></b></li>
<li><b><a href="https://dineshonjava.com/cacheing-in-hibernate-first-level-and/">Cacheing in Hibernate: First Level and Second Level Cache in Hibernate</a></b></li>
<li><b><a href="https://dineshonjava.com/using-query-cache-in-hibernate/">Using Query Cache in Hibernate</a></b></li>
<li><b><a href="https://dineshonjava.com/hibernate-batch-processing_10/">Hibernate Batch Processing </a></b></li>
</ol>
</div>
<p><b><br />
</b> <b> <;<;<a style="font-family: Times, 'Times New Roman', serif;" href="https://dineshonjava.com/p/transient-persistent-and-detached.html">Previous Chapter 24</a><;<; >;>;N<a href="https://dineshonjava.com/p/introducing-hqlhibernate-query-language.html">ext Chapter 26</a>>;>;</b></p>
<p> ;</p>
</div>
<p> ;</p>
</div>

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.