This repository shows how to declare Hibernate entities using Kotlin
APACHE-2.0 License
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.
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
...
}
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>
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>
open
in the compiled bytecode.equals()
, hashCode()
, toString()
in accordance with one of this articles by Vlad Mihalcea or Thorben Janssen.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.