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.
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.
- Enable the all-open plugin as it was described above, because this is the only way to make data classes
- 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.