{"id":1321,"date":"2016-06-24T08:50:59","date_gmt":"2016-06-24T08:50:59","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=1321"},"modified":"2016-06-24T11:06:27","modified_gmt":"2016-06-24T11:06:27","slug":"persisting-detached-objects","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/persisting-detached-objects\/","title":{"rendered":"Persisting Detached Objects"},"content":{"rendered":"<p>There may be times where you want to retrieve data from  DB, do some changes in it and save back the data.In such a time the connection could not be kept open for the whole period of time since db connections are resource intensive.   <\/p>\n<p>So I will fetch the object -> close the Session -> Do some operations -> Open new session again -> Update the Object -> Close the Session.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\tsessionFactory = createSessionFactory();\r\n\tSession objSession = sessionFactory.openSession();\r\n\tobjSession.beginTransaction();\t\t\r\n\tUsers objUser = objSession.get(com.mugil.user.Users.class, 11);\r\n\tobjSession.getTransaction().commit();\r\n\tobjSession.close();\r\n\r\n\tobjUser.setUserName(&quot;Updated&quot;);\r\n\r\n\tobjSession = sessionFactory.openSession();\r\n\tobjSession.beginTransaction();\r\n\tobjSession.update(objUser);\r\n\tobjSession.getTransaction().commit();\r\n\tobjSession.close();\r\n<\/pre>\n<p>Now the way Hibernate works is it first runs the select query for the value which should be changed and updates the value.So 2 queries for the whole process.Now there are chance&#8217;s the value fetched from DB may or may not be changed.So we can do a check which checks the fetched and updated value for uniquess before running the update query.If the fetched data is not changed th update query wont be run.<\/p>\n<p>The  Annotation for that is as below.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n @Entity\r\n @org.hibernate.annotations.Entity(selectBeforeUpdate=true)\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>There may be times where you want to retrieve data from DB, do some changes in it and save back the data.In such a time the connection could not be kept open for the whole period of time since db connections are resource intensive. So I will fetch the object -> close the Session ->&hellip; <a href=\"https:\/\/codethataint.com\/blog\/persisting-detached-objects\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[74],"tags":[],"class_list":["post-1321","post","type-post","status-publish","format-standard","hentry","category-hibernate"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1321","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=1321"}],"version-history":[{"count":2,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1321\/revisions"}],"predecessor-version":[{"id":1323,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1321\/revisions\/1323"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=1321"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=1321"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=1321"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}