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:

2. Create Models

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

Spring Jpa One To Many Relationship

2.1 Company entity

2.2 Product entity

@Entity: Specifies that the class is an entity. This annotation is applied to the entity class.
@Id: Specifies the primary key of an entity.
@OneToMany: Defines a many-valued association with one-to-many multiplicity.
@ManyToOne: Defines a single-valued association to another entity class that has many-to-one multiplicity
@JoinColumn: Specifies a column for joining an entity association or element collection. If the JoinColumn annotation itself is defaulted, a single join column is assumed and the default values apply.

3. Create JPA Repositories

Create 2 interface repositories by extends JpaRepository:


4. Configure datasource & Spring JPA

Open application.properties, configure spring.datasource & spring.jpa:

5. Implement a test Client

Use 2 repository: CompanyRepository & ProductRepository

Implement 3 functions:
clearData() is used to empty 2 tables company & product
saveData() is used to persist entities (Company & Product) to database
showData() is used to load all records (Company & Product) and show all on console.

For saveData(), we have to 2 approach:
-> saveDataWithApproach1(): saving Company objects that include Product list.
-> saveDataWithApproach2(): firstly persist Company entities(not include Product list). Then store Products with the persisted companies.

Full SourceCode:

6. Run & Check results

Build & Run the project with SpringBoot App mode.

– With Approach 1 for saving:
-> Hibernate Logs:

– With Approach 2 for saving:
-> Hibernate Logs:

-> Application’s Logs:

Database data:
company table:

Spring Jpa One To Many Relationship - company results

product table:

Spring Jpa One To Many Relationship - product results

III. SourceCode


By grokonez | April 18, 2017.

Last updated on February 6, 2020.

Related Posts

6 thoughts on “Spring JPA Hibernate One to Many Relationship – SpringBoot + MySQL”

  1. Thanks for the tutorial. Its helped me a lot to learn what is Spring data jpa with spring boot.

    One issue i am facing is for the first time it is executing as expected. but when i am trying to run this example for the second time its is throwing an error saying “company table already exists”. ( i used MySQL)
    It is trying to create the tables. so i changed the properties like below and tried


    still i am getting the same issue.

    could you please help me on this.

    1. Hello Jilani Pathan,

      spring.jpa.generate-ddl is used to generate table, if you set its value is false.
      Mean application will not create tables.
      -> The case spring.jpa.generate-ddl=false is used when you do not need create tables (tables had been created or you want to create it by manually).

      spring.jpa.hibernate.ddl-auto has a range values: {none, validate, update, create-drop}
      -> You need consider each case for using when development and production.

      About the above tutorial,
      -> Mean Spring JPA will auto generate tables if needed.

      So we can see log for first running:

      But the second time running, don’t see the log.

      We don’t set: spring.jpa.hibernate.ddl-auto means it has default value is none.

      -> So please check more!

      More About: “spring.jpa.hibernate.ddl-auto” = create-drop: It will create and drop tables when running your app,
      See logs:

      Hope the info will be useful for you.

  2. How the main program is running and executing a set of methods and then closing on its own? I mean its not implementing any other DisposalBean?

    1. I got it. It was not a web project and hence the application was getting closed immediately after the execution of the methods in run.

  3. Hi

    Thanks for the tutorial, but I am getting an infinite loop while doing One to Many Mapping.
    Please find below details

    product category entity :

    Could you please help me to solve the same.

Got Something To Say:

Your email address will not be published. Required fields are marked *