Spring JPA Hibernate One to Many Relationship - SpringBoot + MySQL
spring jpa
Male avatar

loveprogramming viết ngày 03/04/2021


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:

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:



2. Create Models

2 Entities Company and Product that having One-to-Many relationship:

Spring Jpa 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;

public class Company {
    @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();
        JSONArray productArray = new JSONArray();
        if(this.products != null){
                JSONObject subJson = new JSONObject();
                subJson.put("name", product.getName());
        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

Bình luận

{{ comment.user.name }}
Bỏ hay Hay
Male avatar
{{ comment_error }}

Hiển thị thử

Chỉnh sửa

Male avatar


230 bài viết.
66 người follow
{{userFollowed ? 'Following' : 'Follow'}}
Cùng một tác giả
Male avatar
1 0
Tutorial Link: (Link) (Ảnh) Django is a Pythonbased free and opensource web framework that follows the modeltemplateview architectural pattern. A...
loveprogramming viết 5 tháng trước
1 0
Male avatar
1 0
https://loizenai.com/angular11nodejspostgresqlcrudexample/ Angular 11 Node.js PostgreSQL Crud Example (Ảnh) Tutorial: “Angular 11 Node.js Postg...
loveprogramming viết 4 tháng trước
1 0
Male avatar
1 0
Angular Spring Boot jwt Authentication Example Github https://loizenai.com/angularspringbootjwt/ (Ảnh) Tutorial: ” Angular Spring Boot jwt Authe...
loveprogramming viết 4 tháng trước
1 0
Bài viết liên quan
Male avatar
0 0
https://grokonez.com/frontend/angular/angular6/kotlinspringbootangular6crudhttpclientmysqlexamplespringdatajparestapisexample Kotlin Spring Boot +...
loveprogramming viết 10 ngày trước
0 0


{{ comment_count }}

bình luận

{{liked ? "Đã kipalog" : "Kipalog"}}

Male avatar
{{userFollowed ? 'Following' : 'Follow'}}
230 bài viết.
66 người follow

 Đầu mục bài viết

Vẫn còn nữa! x

Kipalog vẫn còn rất nhiều bài viết hay và chủ đề thú vị chờ bạn khám phá!