Transient, Persistent and Detached Objects in Hibernate

<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<div dir&equals;"ltr" style&equals;"text-align&colon; justify&semi;">&NewLine;<p>In couple of previous chapters we have seen that every entity object are passed to three states of the object before saving and updating the row in the database table as per as given in the following picture&period;<&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2017&sol;04&sol;states&period;jpg" border&equals;"0" &sol;><&sol;div>&NewLine;<&sol;div>&NewLine;<div id&equals;"ads-id" align&equals;"center"><&sol;div>&NewLine;<h2><b>1&period; Transient State&colon;<&sol;b><br &sol;>&NewLine;<b><&sol;b><&sol;h2>&NewLine;<p><b> <&sol;b>A New instance of  a persistent class which is not associated with a <i><b>Session<&sol;b><&sol;i>&comma; has no representation in the <i><b>database <&sol;b><&sol;i>and no identifier value is considered <b><i>transient<&sol;i><&sol;b> by Hibernate&colon;<b> <&sol;b><&sol;p>&NewLine;<&sol;div>&NewLine;<pre class&equals;"highlight">UserDetail user &equals; new UserDetail&lpar;&rpar;&semi; &NewLine;user&period;setUserName&lpar;"Dinesh Rajput"&rpar;&semi; &NewLine;&sol;&sol; user is in a transient state &NewLine;<&sol;pre>&NewLine;<div style&equals;"background-color&colon; &num;f2f9fc&semi; border&colon;1px solid &num;c9e6f2&semi;border-radius&colon;3px&semi;padding&colon;16px&semi;line-height&colon;1&period;45&semi;">&NewLine;<span style&equals;"color&colon; red&semi;font-size&colon; x-large&semi;text-align&colon; center&semi;"><b>Popular Tutorials<&sol;b><&sol;span><&sol;p>&NewLine;<ul style&equals;"text-align&colon; left&semi;">&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-tutorial&sol;"><em><strong>Spring Tutorial<&sol;strong> <&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-web-mvc-framework-chapter-38&sol;"><strong><em>Spring MVC Web Tutorial <&sol;em><&sol;strong><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;introduction-to-spring-boot-a-spring-boot-complete-guide&sol;"><strong>Spring Boot Tutorial<&sol;strong> <&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-security-take-baby-step-to-secure&sol;"><em>Spring Security Tutorial<&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint&sol;"><em>Spring AOP Tutorial<&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;using-spring-jdbc-framework-chapter-32&sol;"><em>Spring JDBC Tutorial<&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-hateoas-hypermedia-driven-restful-web-service&sol;"><em><strong>Spring HATEOAS <&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;microservices-with-spring-boot&sol;"><em><strong>Microservices with Spring Boot<&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;jax-rs-web-service-tutorial&sol;"><strong><em>REST Webservice<&sol;em> <&sol;strong><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;core-java-baby-step-to-be-best-java-ian&sol;"><em><strong>Core Java <&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;hibernate-3-on-baby-steps&sol;"><em><strong>Hibernate Tutorial<&sol;strong><&sol;em><&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;spring-batch-process-with-example&sol;"><strong><em>Spring Batch<&sol;em> <&sol;strong><&sol;a><&sol;b><&sol;li>&NewLine;<&sol;ul>&NewLine;<&sol;div>&NewLine;<h2><b>2&period; Persistent State&colon;<&sol;b><&sol;h2>&NewLine;<p>A persistent instance has a representation in the <i><b>database <&sol;b><&sol;i>&comma; an identifier value and is associated with a <i><b>Session<&sol;b><&sol;i>&period; You can make a transient instance persistent by associating it with a <i><b>Session<&sol;b><&sol;i>&colon;<&sol;p>&NewLine;<pre class&equals;"highlight">Long id &equals; &lpar;Long&rpar; session&period;save&lpar;user&rpar;&semi; &NewLine;&sol;&sol; user is now in a persistent state &NewLine;<&sol;pre>&NewLine;<div style&equals;"background-color&colon; &num;f2f9fc&semi; border-radius&colon; 3px&semi; border&colon; 1px solid &num;c9e6f2&semi; line-height&colon; 1&period;45&semi; padding&colon; 16px 16px 16px 16px&semi;">&NewLine;<h2 dir&equals;"ltr">Spring 5 Design Pattern Book<&sol;h2>&NewLine;<div dir&equals;"ltr" style&equals;"color&colon; &num;20124d&semi; font-family&colon; 'calibri' &comma; sans-serif&semi; font-size&colon; 13pt&semi; text-align&colon; justify&semi;">You could purchase my <strong>Spring 5 book<&sol;strong> that is with title name &&num;8220&semi;<strong>Spring 5 Design Pattern<&sol;strong>&&num;8220&semi;&period; This book is available on the <a href&equals;"https&colon;&sol;&sol;www&period;amazon&period;in&sol;Spring-Design-Patterns-Dinesh-Rajput&sol;dp&sol;1788299450&sol;ref&equals;sr&lowbar;1&lowbar;1&quest;ie&equals;UTF8&amp&semi;qid&equals;1507925340&amp&semi;sr&equals;8-1&amp&semi;keywords&equals;spring&plus;5&plus;design&plus;pattern" target&equals;"&lowbar;blank" rel&equals;"noopener"><strong>Amazon<&sol;strong><&sol;a> and <a href&equals;"https&colon;&sol;&sol;www&period;packtpub&period;com&sol;application-development&sol;spring-5-design-patterns" target&equals;"&lowbar;blank" rel&equals;"noopener"><strong>Packt<&sol;strong><&sol;a> publisher website&period; Learn various <strong>design patterns<&sol;strong> and <strong>best practices<&sol;strong> in Spring 5 and use them to solve common design problems&period; You could use author discount to purchase this book by using code- &&num;8220&semi;<strong>AUTHDIS40<&sol;strong>&&num;8220&semi;&period;<&sol;div>&NewLine;<div dir&equals;"ltr"><&sol;div>&NewLine;<div dir&equals;"ltr"><a href&equals;"https&colon;&sol;&sol;www&period;amazon&period;in&sol;Spring-Design-Patterns-Dinesh-Rajput&sol;dp&sol;1788299450&sol;ref&equals;sr&lowbar;1&lowbar;1&quest;s&equals;books&&num;038&semi;ie&equals;UTF8&&num;038&semi;qid&equals;1512607966&&num;038&semi;sr&equals;1-1&&num;038&semi;keywords&equals;dinesh&plus;rajput" target&equals;"&lowbar;blank" rel&equals;"noopener"><img class&equals;"aligncenter wp-image-3053 size-medium" title&equals;"Spring-5-Design-Pattern" src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2013&sol;03&sol;Spring-5-design-pattern-243x300&period;jpg" width&equals;"243" height&equals;"300" &sol;><&sol;a><&sol;div>&NewLine;<&sol;div>&NewLine;<h2><b>3&period; Detached State&colon;<&sol;b><&sol;h2>&NewLine;<p>Now&comma; if we close the <i><b>Hibernate Session<&sol;b><&sol;i>&comma; the <i><b>persistent<&sol;b><&sol;i> instance will become a <i><b>detached<&sol;b><&sol;i> instance&colon; it isn&&num;8217&semi;t attached to a <i><b>Session<&sol;b><&sol;i> anymore &lpar;but can still be modified and reattached to a new Session later though&rpar;&period;<&sol;p>&NewLine;<pre class&equals;"highlight">session&period;close&lpar;&rpar;&semi; &NewLine;&sol;&sol;user in detached state &NewLine;<&sol;pre>&NewLine;<h2><b>Difference between Transient and Detached States&colon;<&sol;b><&sol;h2>&NewLine;<p>Transient objects do not have association with the databases and session objects&period; They are simple objects and not persisted to the database&period; Once the last reference is lost&comma; that means the object itself is lost&period; And of course &comma; garbage collected&period; The commits and rollbacks will have no effects on these objects&period; They can become into persistent objects through the save method calls of Session object&period;<&sol;p>&NewLine;<p>The detached object have corresponding entries in the database&period; These are persistent and not connected to the Session object&period; These objects have the synchronized data with the database when the session was closed&period; Since then&comma; the change may be done in the database which makes this object stale&period; The detached object can be reattached after certain time to another object in order to become persistent again&period;<&sol;p>&NewLine;<p>Lets see in the following example to save or update the user data&&num;8230&semi;<&sol;p>&NewLine;<pre class&equals;"highlight">package com&period;sdnext&period;hibernate&period;tutorial&semi; &NewLine; &NewLine;import org&period;hibernate&period;Session&semi; &NewLine;import org&period;hibernate&period;SessionFactory&semi; &NewLine;import org&period;hibernate&period;cfg&period;AnnotationConfiguration&semi; &NewLine; &NewLine;import com&period;sdnext&period;hibernate&period;tutorial&period;dto&period;UserDetails&semi; &NewLine; &NewLine;public class HibernateTestDemo &lbrace; &NewLine; &sol;&ast;&ast; &NewLine; &ast; &commat;param args &NewLine; &ast;&sol; &NewLine; public static void main&lpar;String&lbrack;&rsqb; args&rpar; &NewLine; &lbrace; &NewLine; UserDetails userDetails &equals; new UserDetails&lpar;&rpar;&semi; &NewLine; userDetails&period;setUserName&lpar;"Dinesh Rajput"&rpar;&semi; &NewLine; userDetails&period;setAddress&lpar;"Noida City"&rpar;&semi; &NewLine; &sol;&sol;Here 'userDetails' is TRANSIENT object &NewLine; &NewLine; SessionFactory sessionFactory &equals; new AnnotationConfiguration&lpar;&rpar;&period;configure&lpar;&rpar;&period;buildSessionFactory&lpar;&rpar;&semi; &NewLine; Session session &equals; sessionFactory&period;openSession&lpar;&rpar;&semi; &NewLine; session&period;beginTransaction&lpar;&rpar;&semi; &NewLine; &NewLine; session&period;save&lpar;userDetails&rpar;&semi; &NewLine; &sol;&sol;Here 'userDetails' is PERSISTENT object &NewLine; userDetails&period;setUserName&lpar;"User Updated after session close"&rpar;&semi; &NewLine; &NewLine; session&period;getTransaction&lpar;&rpar;&period;commit&lpar;&rpar;&semi; &NewLine; session&period;close&lpar;&rpar;&semi; &NewLine; &sol;&sol;Here 'userDetails' is DETACHED object &NewLine; &rcub; &NewLine;&rcub; &NewLine;<&sol;pre>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<div class&equals;"separator" style&equals;"clear&colon; both&semi; text-align&colon; center&semi;"><img src&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;wp-content&sol;uploads&sol;2017&sol;04&sol;state&period;jpg" width&equals;"640" height&equals;"252" border&equals;"0" &sol;><&sol;div>&NewLine;<p><b>In the <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;p&sol;understanding-state-changes-of-object&period;html">Next Chapter<&sol;a> we will discuss about the <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;p&sol;understanding-state-changes-of-object&period;html">Object states in the Hibernate with example<&sol;a>&period;<&sol;b><br &sol;>&NewLine;<b><br &sol;>&NewLine;<&sol;b> <b>                         &lt&semi;&lt&semi;<a style&equals;"font-family&colon; Times&comma; 'Times New Roman'&comma; serif&semi;" href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;p&sol;crud-operations-using-hibernate-3&period;html">Previous Chapter 23<&sol;a>&lt&semi;&lt&semi;    &gt&semi;&gt&semi;N<a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;p&sol;understanding-state-changes-of-object&period;html">ext Chapter 25<&sol;a>&gt&semi;&gt&semi;<&sol;b><br &sol;>&NewLine;<b><br &sol;>&NewLine;<&sol;b> <b><br &sol;>&NewLine;<&sol;b> <b><br &sol;>&NewLine;<&sol;b> <b><br &sol;>&NewLine;<&sol;b> <b><br &sol;>&NewLine;<&sol;b> <b><br &sol;>&NewLine;<&sol;b> <b><br &sol;>&NewLine;<&sol;b><&sol;p>&NewLine;<&sol;div>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<&sol;div>&NewLine;