Spring JPA Hibernate One to Many Relationship - SpringBoot + MySQL
Tags: springjpa, springboot, mysql
Published: 2021-04-03 12:16:22

https://grokonez.com/spring-framework/spring-data/configure-spring-jpa-one-to-many-relationship-springboot

Spring JPA Hibernate One to Many Relationship - SpringBoot + MySQL

This tutorial will guide you through the steps of configuring Spring JPA <strong>One to Many relationship</strong> with Spring Boot and MySql.

Related articles: 
- <a href="https://grokonez.com/spring-framework/spring-boot/spring-jpa-many-to-many">Spring JPA – Many to Many relationship</a>
- <a href="https://grokonez.com/spring-framework/spring-data/configure-spring-jpa-one-one-relationship-springboot">How to configure Spring JPA One to One Relationship – SpringBoot</a>
- <a href="https://grokonez.com/spring-framework/spring-boot/spring-data-rest-create-restapis-jpa-one-many-relational-entities-springboot-mysql-hal-browser">Spring Data Rest – JPA One-to-Many relational entities | SpringBoot + MySql + HAL Browser</a>
- <a href="https://grokonez.com/spring-framework/spring-data/kotlin-springjpa-hibernate-one-many-relationship">Kotlin SpringJPA Hibernate One-To-Many relationship</a>

<!--more-->
<iframe width="853" height="480" src="https://www.youtube.com/embed/6Hf0_5FJMsU?rel=0" frameborder="0" allowfullscreen></iframe>

<div id="tuttoc" class="jsa-toc"></div>

<div id="tutcontents">
<h2>I. Technologies</h2>
– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.5.6.RELEASE
– MySql database
<h2>II. Practice - Spring JPA One to Many Relationship</h2>
<strong>Step to do</strong>
- Create SpringBoot project
- Create Models
- Create JPA Repositories
- Configure Datasource & Spring JPA
- Implement a test Client
- Run & Check results
<h3>1. Create SpringBoot project</h3>
– Using <strong>SpringToolSuite</strong>, create a SpringBoot project. Then add needed dependencies:
<pre><code class="language-html">&lt;dependency&gt;
	&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
	&lt;artifactId&gt;spring-boot-starter-data-jpa&lt;/artifactId&gt;
&lt;/dependency&gt;

&lt;dependency&gt;
	&lt;groupId&gt;mysql&lt;/groupId&gt;
	&lt;artifactId&gt;mysql-connector-java&lt;/artifactId&gt;
	&lt;scope&gt;runtime&lt;/scope&gt;
&lt;/dependency&gt;

&lt;dependency&gt;
	&lt;groupId&gt;org.json&lt;/groupId&gt;
	&lt;artifactId&gt;json&lt;/artifactId&gt;
&lt;/dependency&gt;</code></pre>
<h3>2. Create Models</h3>
2 Entities <strong>Company</strong> and <strong>Product</strong> that having <strong>One-to-Many</strong> relationship:

<img src="https://grokonez.com/wp-content/uploads/2017/04/Spring-Jpa-One-To-Many-Relationship.png" alt="Spring Jpa One To Many Relationship" width="544" height="208" class="alignnone size-full wp-image-4641" />
<h4>2.1 Company entity</h4>
<pre><code class="language-java">
package com.javasampleapproach.jpa.one2many.model;

import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.json.JSONArray;
import org.json.JSONObject;

@Entity
@Table(name="company")
public class Company {
	@Id
    @GeneratedValue(strategy = GenerationType.AUTO)
	private int id;
    private String name;
    
    @OneToMany(mappedBy = "company", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    private Set<Product> products;
    
    public Company(){
    }
    
    public Company(String name){
    	this.name = name;
    }
    
    // name
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    // products
    public void setProducts(Set<Product> products){
    	this.products = products;
    }
    
    public Set<Product> getProducts(){
    	return this.products;
    }
    
    public String toString(){
    	String info = "";
        JSONObject jsonInfo = new JSONObject();
        jsonInfo.put("name",this.name);
        
        JSONArray productArray = new JSONArray();
        if(this.products != null){
            this.products.forEach(product->{
                JSONObject subJson = new JSONObject();
                subJson.put("name", product.getName());
                productArray.put(subJson);
            });
        }
        jsonInfo.put("products", productArray);
        info = jsonInfo.toString();
        return info;
    }
}</code></pre>

More tutorial: https://grokonez.com/spring-framework/spring-data/configure-spring-jpa-one-to-many-relationship-springboot