<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<p><b>In this section, you will know about the jpa relationship. JPA supports the relationship between entities.</b><br />
<b><br />
JPA Relationship</b><br />
In this section, you will know about the jpa relationship. JPA supports the relationship between entities.</p>
<div id="ads-id" align="center"></div>
<div style="background-color: #f2f9fc; border:1px solid #c9e6f2;border-radius:3px;padding:16px;line-height:1.45;">
<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>
<p><b>There are following types of relationship</b>:</p>
<ol>
<li><b><a href="https://dineshonjava.com/p/hibernate-one-to-one-mapping-tutorial.html">One-to-one</a></b></li>
<li><b><a href="https://dineshonjava.com/p/hibernate-one-to-many-mapping-tutorial.html">One-to-many</a></b></li>
<li><b><a href="https://dineshonjava.com/p/hibernate-many-to-one-mapping-tutorial.html">Many-to-one</a></b></li>
<li><b><a href="https://dineshonjava.com/p/hibernate-many-to-many-mapping-tutorial.html">Many-to-many</a></b></li>
</ol>
<p>JPA relationships are not bi-directional. JPA annotations are added to define the relationship. If, you want to make a relationship bi-directional to use a special attribute.</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&#038;ie=UTF8&#038;qid=1512607966&#038;sr=1-1&#038;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>
<ol style="text-align: left;">
<li><b>One-to-one</b>: A <b> OneToOne </b>relation mapping to a single-value association to another entity. It has one-to-one multiplicity and infers the associated target entity from the type of the object being referenced.<b> Use of the @OneToOne Annotation</b>:
<ol>
<li>Configure the fetch type to LAZY</li>
<li>Configure the mapping to forbid null values (for non-primitive types) in case null values are inappropriate for your application</li>
<li>Configure the associated target entity if it cannot be inferred from the type of the object being referenced</li>
<li>Configure the operations that must be cascaded to the target of the association. For example, if the owning entity is removed, ensure that the target of the association is also removed</li>
</ol>
</li>
<li><b>One-to-many</b>: JPA defines a OneToMany mapping for a many-valued association with one-to-many multiplicity.<b> Use of the @OneToMany Annotation</b>:
<ol>
<li>Configure the fetch type to EAGER</li>
<li>Configure the associated target entity because the Collection used is not defined using generics</li>
<li>Configure the operations that must be cascaded to the target of the association: for example, if the owning entity is removed, ensure that the target of the association is also removed</li>
<li>Configure the details of the join table used by TopLink JPA for uni-directional one-to-many relationships</li>
</ol>
</li>
<li><b>Many-to-one</b>: JPA defines a ManyToOne mapping for a single-valued association to another entity class that has many-to-one multiplicity.Use the <b> @ManyToOne</b> Annotation to:
<ol>
<li>Configure the fetch type to LAZY</li>
<li>Configure the mapping to forbid null values (for non-primitive types) in case null values are inappropriate for your application</li>
<li>Configure the associated target entity if it cannot be inferred from the type of the object being referenced</li>
<li>Configure the operations that must be cascaded to the target of the association: for example, if the owning entity is removed, ensure that the target of the association is also removed</li>
</ol>
</li>
<li><b>Many-to-many</b>: JPA defines a <b> @ManyToMany</b> mapping for a many-valued association with many-to-many multiplicity.<b> Use of the @ManyToMany annotation</b>:
<ol>
<li>Declare the cardinality of the relationship</li>
<li>Configure the fetch type to EAGER</li>
<li>Configure the mapping to forbid null values (for non-primitive types) in case null values are inappropriate for your application</li>
<li>Configure the associated target entity because the Collection used is not defined using generics</li>
<li>Configure the operations that must be cascaded to the target of the association; for example, if the owning entity is removed, ensure that the target of the association is also removed</li>
</ol>
<div style="margin-left: 280px;"></div>
<p><b>There are following types of cascade</b>:</p>
<ol>
<li> <b>CascadeType.PERSIST</b>: When we persist and entity all the entities held in this field persist too. If you want to persist an entity and the fields dont use it fails.<b> </b></li>
<li><b>CascadeType.REMOVE</b>: When we delete an entity all the entities held in this field delete too.</li>
<li><b> CascadeType.REFRESH</b>: When we refresh an entity all the entities held in this field refresh too.</li>
<li><b> CascadeType.MERGE</b>: When we merde an entity all the entities held in this flied merged too</li>
</ol>
</li>
</ol>
<p>The property <b> cascade = CascadeType.ALL</b> indicates that when we persist, remove, refresh or merge this entity all the entities held in this field would be persist, remove, delete or update.</p>
<p><b>In the <a href="https://dineshonjava.com/p/hibernate-many-to-many-mapping-tutorial.html">Next Chapter</a> we will learn how to implements <a href="https://dineshonjava.com/p/implementing-inheritance-in-hibernate.html">inheritance in the Hibernate</a>.</b><br />
<b><br />
</b> <b> <;<;<a style="font-family: Times, 'Times New Roman', serif;" href="https://dineshonjava.com/p/hibernate-many-to-many-mapping-tutorial.html">Previous Chapter 20</a><;<; >;>;N<a style="font-family: Times, 'Times New Roman', serif;" href="https://dineshonjava.com/p/implementing-inheritance-in-hibernate.html">ext Chapter22>;>;</a></b></p>
<p> ;</p>
</div>
<p> ;</p>
</div>

View Comments
Hi dinesh,
can you please post cascade full example code