Technologies used:   Kotlin 1.2.10 | Spring 5 | Maven 3.3.9 | Spring-boot 2.0.0.M7

In this first post, we will study the use of Spring 5 MVC and JPA with Kotlin, on the internet, there is a bunch of examples with Java, but nearly nothing for Kotlin. I will try with this post that Kotlin, could be a great candidate to replace Java, even on Spring 5 projects.

One thing that it is great, Spring 5 integrate Kotlin in this project and now it is fully supported.

Initialise the Project

Before to start we will use Spring-boot, for this, we are going to the URL:

Screenshot 2017-10-23 15.47.08

From the screen “Spring initializr” we select Kotlin as language and we will use Spring Boot 2.0.0 M5 as at that time it is already in a release candidate. Also, open the advanced menu and select the following:

  • On Web: select Web
  • On SQL: select JPA and H2
  • On Template: select Tymleaf

After completing the Group and the artifact click on “Generate Project”, download the project, unzip and then open in your favorite IDE. For my part, I will open under Intelij Ultimate as the community doesn’t include Spring plugin.

Start to write your first JPA

Under your maven source folder, src/main you should found the folder Kotlin. Under this, I created 3 packages:

Screenshot 2017-10-23 17.08.46

We will use for the moment only the model package

Create a Kotlin class: Author and Book as follow:

package vn.finixasia.springframework.spring5webapp.model

data class Author constructor(var firstName: String, var lastName: String)

and Book.kt

package vn.finixasia.springframework.spring5webapp.model

data class Book constructor(var title: String, var isbn: String, var publisher: String)

In Kotlin, make a data model is quite strait-worth, you just need to write a class of type data with a default constructor that contains each property,  you differentiate between a read-only (val) and a mutable (var). That’s it it’s quite simple.

Now to make them understand by Spring JPA, you use the annotation @Entity from java.persistence like that:

package vn.finixasia.springframework.spring5webapp.model

import javax.persistence.*

data class Book constructor(var title: String, var isbn: String, var publisher: String)

Now hibernate understand it is an entity.

On our example, a Book as many to many Authors, so we need to add first an Id and then a field authors that will contain all the authors names as follow:

@GeneratedValue(strategy = GenerationType.AUTO)
var id: Long = 0

var authors:MutableSet<Author> = HashSet()

Beware Kotlin make the difference between a mutable or not mutable interface, so if you come from Java, select the MutableSet<> instead the non-mutable Set<>

We do the same on the data model Author as an Author has many to many books

package vn.finixasia.springframework.spring5webapp.model

import javax.persistence.*

data class Author constructor(var firstName: String, var lastName: String) {
@GeneratedValue(strategy = GenerationType.AUTO)
var id: Long = 0

@ManyToMany(mappedBy = "authors")
var books: MutableSet<Book> = HashSet()

Just before watching the result, open in your resources folder and set to true: 


now you can launch your h2 console, to see the result:

./mvnw spring-boot:run

and go to the url: http://localhost:8080/h2-console

Screenshot 2017-10-23 19.29.20

Just click Connect and you should see your table, if you don’t see them, it is most probably because the JDBC URL was wrong, check it, sometimes the cache makes some weird changes.

Let’s bring some testing data

An easy way to add data into, it’s to use the JPA Repository, here again, it’s easy

  1. Create an AuthorRepository Kotlin Interface under the repositories package
  2. Implement<Author, Long>
  3. Create a BookRepository Kotlin Interface under the repositories package
  4. Implement<Book, Long>
  5. Create a DevBootstrap class under bootstrap package
  6. Implement as follow
package vn.finixasia.springframework.spring5webapp.bootstrap

import javafx.application.Application
import org.springframework.context.ApplicationListener
import org.springframework.context.event.ContextRefreshedEvent
import org.springframework.stereotype.Component
import vn.finixasia.springframework.spring5webapp.model.Author
import vn.finixasia.springframework.spring5webapp.model.Book
import vn.finixasia.springframework.spring5webapp.repositories.AuthorRepository
import vn.finixasia.springframework.spring5webapp.repositories.BookRepository

class DevBootstrap constructor(
        var authorRepository: AuthorRepository
        , var bookRepository: BookRepository)
    : ApplicationListener<ContextRefreshedEvent> {

    override fun onApplicationEvent(p0: ContextRefreshedEvent?) {

    private fun initData() {

        var eric = Author("Eric", "Evans")
        var ddd = Book("Domain Driven Design", "1234","Harper Collins")

This is, if now you refresh the console, you have data in your tables.

Kotlin Spring 5: JPA + MVC Part 2