This repository shows how to declare Hibernate entities using Kotlin

Overview

JPA Entities in Kotlin

This repository was originally used in the "Getting the Most from JPA with Kotlin" webinar. To follow the webinar flow step by step, checkout the "webinar-starting-point" branch and click on the image below to start watching.

Watch the video

JPA Entities on Kotlin Checklist

  • To avoid significant performace issues and enable lazy loading for Many/One to One assosiations make sure you mark all JPA-related classes and their properties as open.
@Table(name = "project")
@Entity
open class Project {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    open var id: Long? = null

    @Column(name = "name", nullable = false)
    open var name: String? = null

    ...
}
  • You may use the all-open compiler plugin to make all JPA-related classes and properties open by default. Make sure you configure it right, so it applys for all classes annotated as @Entity, @MappedSuperclass, @Embeddable.
<compilerPlugins>
   ...
   <plugin>all-open</plugin>
   ...
</compilerPlugins>
<pluginOptions>
   <option>all-open:annotation=javax.persistence.Entity</option>
   <option>all-open:annotation=javax.persistence.MappedSuperclass</option>
   <option>all-open:annotation=javax.persistence.Embeddable</option>
</pluginOptions> 
  • Using primary constructors in JPA-related classes will cause the following exception org.hibernate.InstantiationException: No default constructor for entity. To resolve this issue you may manually define no-args constructor or use the kotlin-jpa compiler plugin, which ensures that no-args constructor will be generated in bytecode for each JPA-related class.
<compilerPlugins>
   ...
   <plugin>jpa</plugin>
   ...   
</compilerPlugins>
  • To use data classes
    • Enable the all-open plugin as it was described above, because this is the only way to make data classes open in the compiled bytecode.
    • Override equals(), hashCode(), toString() in accordance with one of this articles by Vlad Mihalcea or Thorben Janssen.
  • JPA Buddy is aware of all these things and always generate valid entities for you, including extra stuff like equals(), hashCode(), toString()!

Find more examples of JPA entities on Kotlin in the domain.kt file. It also includes detailed explanation of the different use cases inlined in the comments.

You might also like...
A reliable android app that shows upcoming fixtures, updated league tables, and top goal scorers in a league
A reliable android app that shows upcoming fixtures, updated league tables, and top goal scorers in a league

SwiftScore A football stats and livescore app built with Kotlin,MVVM architecture,Retrofit, Coroutines and Navigation Components Demo Screenshots API

sample project that shows you how you can use Ktor to creat a server for real Project.

Ktor-Sample This is a sample project that shows you how you can use Ktor to creat a server for real Project. What is done Save data to database (Get a

A very simple Android app which shows you random memes with the help of meme-api which you can share with your friends!
A very simple Android app which shows you random memes with the help of meme-api which you can share with your friends!

Meme Share A very simple Android app which shows you random memes with the help of meme-api which you can share with your friends! Tech stack 100% wri

>On this day< is a JetBrains Space app which shows Wikipedia-based information about what happened on that or given day
On this day is a JetBrains Space app which shows Wikipedia-based information about what happened on that or given day

kotlin-spaces-app-onthisday On this day is a JetBrains Space app which shows Wikipedia-based information about what happened on that or given day Stat

❤️ A sample Marvel heroes application based on MVVM (ViewModel, Coroutines, LiveData, Room, Repository, Koin)  architecture.
❤️ A sample Marvel heroes application based on MVVM (ViewModel, Coroutines, LiveData, Room, Repository, Koin) architecture.

MarvelHeroes MarvelHeroes is a demo application based on modern Android application tech-stacks and MVVM architecture. Fetching data from the network

It is a repository containing backend structure for Ktor.
It is a repository containing backend structure for Ktor.

Backend Architecture with Ktor + KMongo This project contains, Authentication using Jwt Database Layer (KMongo - Orm for MongoDB) Routing Advanced Rou

This repository contains RabbitMQ Protobuf starters with its usage samples for spring-rabbit and spring-cloud-starter-stream-rabbit modules

This repository contains RabbitMQ Protobuf starters with its usage samples for spring-rabbit and spring-cloud-starter-stream-rabbit modules

This repository is a simple humidity and temperature dashboard to present data from sensors on your phone
This repository is a simple humidity and temperature dashboard to present data from sensors on your phone

ChilliBook This repository is a simple humidity and temperature dashboard to present data from sensors on your phone. It uses Bluetooth LE and an ESP3

This repository contains the Android Frontend part of our project

Android_Frontend This repository contains the Android Frontend part of our project. In this part of the project many extra features could be added. Bu

Owner
JPA Buddy
JPA Buddy is an advanced IntelliJ IDEA plugin intended to accelerate and simplify using JPA and related tech: Hibernate, Spring Data JPA, Liquibase, Flyway...
JPA Buddy
Kotlin microservices with REST, and gRPC using BFF pattern. This repository contains backend services. Everything is dockerized and ready to "Go" actually "Kotlin" :-)

Microservices Kotlin gRPC Deployed in EC2, Check it out! This repo contains microservices written in Kotlin with BFF pattern for performing CRUD opera

Oguzhan 18 Apr 21, 2022
Kotlin app that shows a list of characters using the api SWAPI

StarWarsCharacters Kotlin app that shows a list of characters using the api SWAPI (https://swapi.dev) This is a clean architecture app example built w

Lorenzo 3 Jun 2, 2021
🦁 A Disney app using transformation motions based on MVVM (ViewModel, Coroutines, Flow, LiveData, Room, Repository, Koin) architecture.

DisneyMotions A demo Disney app using transformation motions based on MVVM architecture. The motion system is included in the 1.2.0-alpha05 released m

Jaewoong Eum 1.4k Jan 2, 2023
PlaceAutoComplete - Practice Repository for placesearch using google places Api

PlaceAutoComplete Practice Repository for placesearch using google places Api Ap

Nayeem Shiddiki Abir 0 Jan 1, 2022
Mobile App that shows Random Products in a Nested Recyclerview for Android [Kotlin] Developer Role at Edvora

EdvoraAndroidTest An simple Android App for Edvora Android Assessment. It consum

Joel Kanyi 6 Jul 12, 2022
Repository with source code from http://rosettacode.org/wiki/Category:Kotlin

Rosetta Code Kotlin This is a repository with the Kotlin source code from RosettaCode wiki. The main motivation for extracting all the code into a rep

Dmitry Kandalov 20 Dec 27, 2022
A repository full of Forge 1.8.9 Kotlin utilities

kotlin-forge-api kotlin-forge-api is a repository full of different APIs to be used by mods for Forge 1.8.9 to make modding easier! Warning! To use an

Shalom Ademuwagun 7 Sep 28, 2022
This repository demonstrates how Kotlin can simplify Spring Boot configuration properties file mapping

Kotlin spring-boot nested config props This repository demonstrates how Kotlin can simplify Spring Boot configuration properties file mapping @Constru

Maksim Kostromin 1 Oct 11, 2021
Android app that shows what happened today in the history.

Today History Android app Master: Develop: Code Coverage: App that shows what happened today in history. Details Written in Kotlin. Android Studio 2.1

MakinGIANTS 29 Oct 3, 2022
A webapp which generates a simple Discord profile banner image in real-time which shows user's status and activity.

DiscordProfileBanner This tool generates a Discord profile banner image in realtime. I wrote it for use in my AniList profile. An example in action: H

Quanta 11 Oct 17, 2022