Kotlin SpringJPA Hibernate One-To-Many relationship

In the tutorial, JavaSampleApproach will show you how to implement Kotlin SpringJPA Hibernate One-To-Many relationship application with SpringBoot.

Related posts:
Kotlin SpringJPA Many-To-Many relationship
How to configure Spring JPA One to Many Relationship – SpringBoot
Kotlin SpringJPA One-to-One relationship
Kotlin Spring MVC RequestMapping RESTful APIs with @GetMapping, @PostMapping, @PutMapping, @DeleteMapping | SpringBoot Example

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
– Kotlin language

II. Practice

In the tutorial, We create a Kotlin SpringBoot project that have 2 entities Company and Product with One-To-Many relationship as below:

kotlin springjpa hibernate one-to-many - relationship uml

Project Structure:

kotlin springjpa hibernate one-to-many - project structure

Step to do:
– Create Kotlin SpringBoot project
– Create Kotlin data models
– Create Spring JPA repositories
– Implement RestController
– Run and check results

1. Create Kotlin SpringBoot project

Using SpringToolSuite to create a Kotlin SpringBoot project.

>>> More step details, you can follow below tutorial:

Then add needed dependencies:

2. Create Kotlin data models

Create 2 Kotlin data model:



3. Create Spring JPA repositories

Using interface JpaRepository to create 2 repositories:

interface CompanyRepository:

interface ProductRepository

@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.

Now open application.properties file, add configuration:

4. Implement RestController

Implement a Kotlin RestController RestAPIsController with 4 APIs:

– For saving entities, We implement 2 APIs with different approaches
-> /api/save/1: Firstly, do saving Company entities. Then do saving Product entities.
-> /api/save/2: Saving Company entities that had attached Product entities.

– For retrieve entities, We implement 2 APIs
-> /api/companies: fetch all Company entities from database.
-> /api/products: fetch all Product entities from database.

5. Run and check results

Build and Run the Kotlin SpringBoot project by commanlines {mvn clean install, mvn spring-boot:run}.

– Make a saving request: http://localhost:8080/api/save/1

kotlin springjpa hibernate one-to-many - save 1

-> Hibernate’s Logs:

– You can try with another saving request: http://localhost:8080/api/save/2

kotlin springjpa one-to-many - save 2

-> Hibernate’s Logs:

-> Check database’s results:

kotlin springjpa one-to-many - db results

– Retrieve Company entities: http://localhost:8080/api/companies
-> Result:
kotlin springjpa one-to-many - retrieve companies

– Retrieve Product entities: http://localhost:8080/api/products
-> Results:

kotlin springjpa one-to-many - retrieve products

III. Sourcecode


By grokonez | September 5, 2017.

Related Posts

1 thought on “Kotlin SpringJPA Hibernate One-To-Many relationship”

Got Something To Say:

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