{"id":1295,"date":"2016-06-22T10:06:44","date_gmt":"2016-06-22T10:06:44","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=1295"},"modified":"2016-06-22T10:06:44","modified_gmt":"2016-06-22T10:06:44","slug":"hibernate-cascade","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/hibernate-cascade\/","title":{"rendered":"Hibernate Cascade"},"content":{"rendered":"<p>Cascade is used when multiple objects in entities wants to be saved in a single shot.<\/p>\n<p><em>For Example<\/em><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n session.save(objUserDetail1);\r\n session.save(objVeh1);\r\n session.save(objVeh2);\r\n session.save(objVeh3);\r\n<\/pre>\n<p>can be replaced by <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n session.persist(objUserDetail1);\r\n<\/pre>\n<p><strong>UserDetails.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n@Entity\r\n@Table(name=&quot;USER_DETAIL&quot;)\r\npublic class UserDetails \r\n{\t\r\n\t@Id @GeneratedValue(strategy = GenerationType.AUTO)\t\r\n\tprivate int UserId;\t\r\n\tprivate String UserName;\r\n\t\r\n\t@OneToMany(cascade=CascadeType.PERSIST)\r\n\tprivate List&lt;Vehicles&gt; arrVehicles = new ArrayList&lt;Vehicles&gt;();\r\n\t  \r\n\tpublic List&lt;Vehicles&gt; getArrVehicles() {\r\n\t\treturn arrVehicles;\r\n\t}\r\n\tpublic void setArrVehicles(List&lt;Vehicles&gt; arrVehicles) {\r\n\t\tthis.arrVehicles = arrVehicles;\r\n\t}\r\n\t\r\n\tpublic int getUserId() {\r\n\t\treturn UserId;\r\n\t}\r\n\tpublic void setUserId(int userId) {\r\n\t\tUserId = userId;\r\n\t}\r\n\tpublic String getUserName() {\r\n\t\treturn UserName;\r\n\t}\r\n\tpublic void setUserName(String userName) {\r\n\t\tUserName = userName;\r\n\t}\r\n}\r\n<\/pre>\n<p><strong>CreateTables.java<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\npublic static void main(String&#x5B;] args) \r\n  {\t\r\n\tUserDetails objUserDetail1 =  new UserDetails();\r\n\tobjUserDetail1.setUserName(&quot;Mugil&quot;);\r\n\r\n\tVehicles objVeh1 = new Vehicles();\r\n\tobjVeh1.setVehicleName(&quot;Suzuki&quot;);\r\n\tobjUserDetail1.getArrVehicles().add(objVeh1);\r\n\r\n\tVehicles objVeh2 = new Vehicles();\r\n\tobjVeh2.setVehicleName(&quot;Maruthi&quot;);\r\n\tobjUserDetail1.getArrVehicles().add(objVeh2);\r\n\r\n\tVehicles objVeh3 = new Vehicles();\r\n\tobjVeh3.setVehicleName(&quot;Volkswagon&quot;);\r\n\tobjUserDetail1.getArrVehicles().add(objVeh3);\r\n\t\t\t\t\t\t\t\r\n\tSessionFactory sessionFact = createSessionFactory();\r\n\tSession session = sessionFact.openSession();\r\n\r\n\tsession.beginTransaction();\t\t\t\t\r\n\tsession.persist(objUserDetail1);\t\t\r\n\r\n\tsession.getTransaction().commit();\r\n\tsession.close();  \r\n  }\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Cascade is used when multiple objects in entities wants to be saved in a single shot. For Example session.save(objUserDetail1); session.save(objVeh1); session.save(objVeh2); session.save(objVeh3); can be replaced by session.persist(objUserDetail1); UserDetails.java @Entity @Table(name=&quot;USER_DETAIL&quot;) public class UserDetails { @Id @GeneratedValue(strategy = GenerationType.AUTO) private int UserId; private String UserName; @OneToMany(cascade=CascadeType.PERSIST) private List&lt;Vehicles&gt; arrVehicles = new ArrayList&lt;Vehicles&gt;(); public List&lt;Vehicles&gt; getArrVehicles() {&hellip; <a href=\"https:\/\/codethataint.com\/blog\/hibernate-cascade\/\">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-1295","post","type-post","status-publish","format-standard","hentry","category-hibernate"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1295","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=1295"}],"version-history":[{"count":1,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1295\/revisions"}],"predecessor-version":[{"id":1296,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/1295\/revisions\/1296"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=1295"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=1295"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=1295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}