Kotlin SpringJPA One-to-One relationship

In the tutorial, JavaSampleApproach will show you the way to create an Kotlin SpringJPA One-to-One relationship application with SpringBoot.

Related posts:
Kotlin SpringJPA Many-To-Many relationship
Kotlin SpringJPA Hibernate One-To-Many relationship
How to configure Spring JPA One to One Relationship – SpringBoot

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 {Wife, Husband} with One-To-One relationship as below:

kotlin springjpa one-to-one - relationship

Project Structure:

kotlin springjpa one-to-one - 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:

Wife.kt:

Husband.kt:

3. Create Spring JPA repositories

Using interface JpaRepository to create 2 repositories:

interface WifeRepository:

interface HusbandRepository:

@Entity: Specifies that the class is an entity. This annotation is applied to the entity class.
@Id: Specifies the primary key of an entity.
@OneToOne: Defines a single-valued association to another entity that has one-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 delete all data from tables, we create an API: /api/clear
– For saving entities, We implement an API: /api/save
– For retrieve entities, We implement 2 APIs
-> /api/students: fetch all Student entities from database.
-> /api/subjects: fetch all Subject entities from database.

5. Run and check results

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

– To make sure a clean database tables before working, we make a clear request: http://localhost:8080/api/clear

kotlin springjpa one-to-one - clear request

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

kotlin springjpa one-to-one - save request

-> Check database’s results:

kotlin springjpa one-to-one - all tables

– Retrieve Wife entities: http://localhost:8080/api/wifes
-> Result:

kotlin springjpa one-to-one - wifes request

– Retrieve Husband entities: http://localhost:8080/api/husbands
-> Result:

kotlin springjpa one-to-one - husband request

III. Sourcecode

KotlinSpringJPAOneToOne


Related Posts


Got Something To Say:

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

*