How to configure Spring JPA One to Many Relationship – SpringBoot

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 – How to create a RestAPIs with JPA One-to-Many relational entities | SpringBoot + MySql + HAL Browser

I. Technologies

– Java 1.8
– Maven 3.3.9
– Spring Tool Suite – Version 3.8.1.RELEASE
– Spring Boot: 1.5.1.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, 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.

Full SourceCode:

6. Run & Check results

Build & Run the project with SpringBoot App mode.
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


Related Posts

2 thoughts on “How to configure Spring JPA One to Many Relationship – SpringBoot”

  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.

Got Something To Say:

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