CascadeTypes 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;<p><b>In this section&comma; you will know about the jpa relationship&period; JPA supports the relationship between entities&period;<&sol;b><br &sol;>&NewLine;<b><br &sol;>&NewLine;JPA Relationship<&sol;b><br &sol;>&NewLine;In this section&comma; you will know about the jpa relationship&period; JPA supports the relationship between entities&period;<&sol;p>&NewLine;<div id&equals;"ads-id" align&equals;"center"><&sol;div>&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;<p><b>There are following types of relationship<&sol;b>&colon;<&sol;p>&NewLine;<ol>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;p&sol;hibernate-one-to-one-mapping-tutorial&period;html">One-to-one<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;p&sol;hibernate-one-to-many-mapping-tutorial&period;html">One-to-many<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;p&sol;hibernate-many-to-one-mapping-tutorial&period;html">Many-to-one<&sol;a><&sol;b><&sol;li>&NewLine;<li><b><a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;p&sol;hibernate-many-to-many-mapping-tutorial&period;html">Many-to-many<&sol;a><&sol;b><&sol;li>&NewLine;<&sol;ol>&NewLine;<p>JPA relationships are not bi-directional&period; JPA annotations are added to define the relationship&period; If&comma; you want to make a relationship bi-directional to use a special attribute&period;<&sol;p>&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;<ol style&equals;"text-align&colon; left&semi;">&NewLine;<li><b>One-to-one<&sol;b>&colon; A <b> OneToOne <&sol;b>relation mapping to a single-value association to another entity&period; It has one-to-one multiplicity and infers the associated target entity from the type of the object being referenced&period;<b> Use of the &commat;OneToOne Annotation<&sol;b>&colon;&NewLine;<ol>&NewLine;<li>Configure the fetch type to LAZY<&sol;li>&NewLine;<li>Configure the mapping to forbid null values &lpar;for non-primitive types&rpar; in case null values are inappropriate for your application<&sol;li>&NewLine;<li>Configure the associated target entity if it cannot be inferred from the type of the object being referenced<&sol;li>&NewLine;<li>Configure the operations that must be cascaded to the target of the association&period; For example&comma; if the owning entity is removed&comma; ensure that the target of the association is also removed<&sol;li>&NewLine;<&sol;ol>&NewLine;<&sol;li>&NewLine;<li><b>One-to-many<&sol;b>&colon; JPA defines a OneToMany mapping for a many-valued association with one-to-many multiplicity&period;<b> Use of the &commat;OneToMany Annotation<&sol;b>&colon;&NewLine;<ol>&NewLine;<li>Configure the fetch type to EAGER<&sol;li>&NewLine;<li>Configure the associated target entity because the Collection used is not defined using generics<&sol;li>&NewLine;<li>Configure the operations that must be cascaded to the target of the association&colon; for example&comma; if the owning entity is removed&comma; ensure that the target of the association is also removed<&sol;li>&NewLine;<li>Configure the details of the join table used by TopLink JPA for uni-directional one-to-many relationships<&sol;li>&NewLine;<&sol;ol>&NewLine;<&sol;li>&NewLine;<li><b>Many-to-one<&sol;b>&colon; JPA defines a ManyToOne mapping for a single-valued association to another entity class that has many-to-one multiplicity&period;Use the <b> &commat;ManyToOne<&sol;b> Annotation to&colon;&NewLine;<ol>&NewLine;<li>Configure the fetch type to LAZY<&sol;li>&NewLine;<li>Configure the mapping to forbid null values &lpar;for non-primitive types&rpar; in case null values are inappropriate for your application<&sol;li>&NewLine;<li>Configure the associated target entity if it cannot be inferred from the type of the object being referenced<&sol;li>&NewLine;<li>Configure the operations that must be cascaded to the target of the association&colon; for example&comma; if the owning entity is removed&comma; ensure that the target of the association is also removed<&sol;li>&NewLine;<&sol;ol>&NewLine;<&sol;li>&NewLine;<li><b>Many-to-many<&sol;b>&colon; JPA defines a <b> &commat;ManyToMany<&sol;b> mapping for a many-valued association with many-to-many multiplicity&period;<b> Use of the &commat;ManyToMany annotation<&sol;b>&colon;&NewLine;<ol>&NewLine;<li>Declare the cardinality of the relationship<&sol;li>&NewLine;<li>Configure the fetch type to EAGER<&sol;li>&NewLine;<li>Configure the mapping to forbid null values &lpar;for non-primitive types&rpar; in case null values are inappropriate for your application<&sol;li>&NewLine;<li>Configure the associated target entity because the Collection used is not defined using generics<&sol;li>&NewLine;<li>Configure the operations that must be cascaded to the target of the association&semi; for example&comma; if the owning entity is removed&comma; ensure that the target of the association is also removed<&sol;li>&NewLine;<&sol;ol>&NewLine;<div style&equals;"margin-left&colon; 280px&semi;"><&sol;div>&NewLine;<p><b>There are following types of cascade<&sol;b>&colon;<&sol;p>&NewLine;<ol>&NewLine;<li>  <b>CascadeType&period;PERSIST<&sol;b>&colon; When we persist and entity all the entities held in this field persist too&period; If you want to persist an entity and the fields dont use it fails&period;<b> <&sol;b><&sol;li>&NewLine;<li><b>CascadeType&period;REMOVE<&sol;b>&colon; When we delete an entity all the entities held in this field delete too&period;<&sol;li>&NewLine;<li><b> CascadeType&period;REFRESH<&sol;b>&colon; When we refresh an entity all the entities held in this field refresh too&period;<&sol;li>&NewLine;<li><b> CascadeType&period;MERGE<&sol;b>&colon; When we merde an entity all the entities held in this flied merged too<&sol;li>&NewLine;<&sol;ol>&NewLine;<&sol;li>&NewLine;<&sol;ol>&NewLine;<p>The property <b> cascade &equals; CascadeType&period;ALL<&sol;b> indicates that when we persist&comma; remove&comma; refresh or merge this entity all the entities held in this field would be persist&comma; remove&comma; delete or update&period;<&sol;p>&NewLine;<p><b>In the <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;p&sol;hibernate-many-to-many-mapping-tutorial&period;html">Next Chapter<&sol;a> we will learn how to implements <a href&equals;"https&colon;&sol;&sol;dineshonjava&period;com&sol;p&sol;implementing-inheritance-in-hibernate&period;html">inheritance in the Hibernate<&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;hibernate-many-to-many-mapping-tutorial&period;html">Previous Chapter 20<&sol;a>&lt&semi;&lt&semi;    &gt&semi;&gt&semi;N<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;implementing-inheritance-in-hibernate&period;html">ext Chapter22&gt&semi;&gt&semi;<&sol;a><&sol;b><&sol;p>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<&sol;div>&NewLine;<p>&nbsp&semi;<&sol;p>&NewLine;<&sol;div>&NewLine;

View Comments