Kotlin SpringJPA Many-To-Many relationship

In the tutorial, JavaSampleApproach will show you the way to create an Kotlin Spring Many-To-Many relationship application with SpringBoot.

Related posts:
Spring JPA – Many to Many relationship
Kotlin SpringJPA Hibernate One-To-Many relationship
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 Student and Subject with Many-To-Many relationship as below:

kotlin springjpa many-to-many - relationship

Project Structure:

kotlin springjpa many-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:

Student.kt:

Subject.kt

3. Create Spring JPA repositories

Using interface JpaRepository to create 2 repositories:

interface StudentRepository

interface SubjectRepository

@Entity: Specifies that the class is an entity. This annotation is applied to the entity class.
@Id: Specifies the primary key of an entity.
@ManyToMany: Defines a many-valued association with many-to-many multiplicity
@JoinTable: Used in the mapping of associations. It is specified on the owning side of an association.
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 hibernate many-to-many - request - clear data

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

kotlin springjpa hibernate many-to-many - request - save data

-> Check database’s results:

kotlin springjpa hibernate many-to-many - all database after saving

– Retrieve Student entities: http://localhost:8080/api/students
-> Result:

kotlin springjpa hibernate many-to-many - request - students

– Retrieve Subject entities: http://localhost:8080/api/subjects
-> Result:

kotlin springjpa hibernate many-to-many - request - subjects

– Hibernate’s Logs:

III. Sourcecode

KotlinSpringJPAManyToMany


Related Posts


Got Something To Say:

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

*