Bạn có chắc chắn muốn xóa bài viết này không ?
Bạn có chắc chắn muốn xóa bình luận này không ?
Spring JPA Hibernate One to Many Relationship - SpringBoot + MySQL
Spring JPA Hibernate One to Many Relationship - SpringBoot + MySQL
This tutorial will guide you through the steps of configuring Spring JPA One to Many relationship with Spring Boot and MySql.
Related articles:
- Spring JPA – Many to Many relationship
- How to configure Spring JPA One to One Relationship – SpringBoot
- Spring Data Rest – JPA One-to-Many relational entities | SpringBoot + MySql + HAL Browser
- Kotlin SpringJPA Hibernate One-To-Many relationship
I. Technologies
– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.5.6.RELEASE
– MySql database
II. Practice - Spring JPA One to Many Relationship
Step to do
- Create SpringBoot project
- Create Models
- Create JPA Repositories
- Configure Datasource & Spring JPA
- Implement a test Client
- Run & Check results
1. Create SpringBoot project
– Using SpringToolSuite, create a SpringBoot project. Then add needed dependencies:<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
</dependency>
2. Create Models
2 Entities Company and Product that having One-to-Many relationship:
2.1 Company entity
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 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 products){
this.products = products;
}
public Set 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;
}
}
More tutorial: https://grokonez.com/spring-framework/spring-data/configure-spring-jpa-one-to-many-relationship-springboot





