Previously, we learned how to define the basic unit of persistence—an entity, using a Kotlin data class. Let's now look at how we can define and map relations to the domain entities using JPA.
Modeling JPA entities
Mapping entities
We can establish a mapping between the entities using JPA's @OneToOne, @OneToMany, @ManyToOne, and @ManyToMany annotations.
Let's consider PersonEntity:
@Entity
class Person : Identity() {
@Id
lateinit var identifier: UUID
lateinit var name: PersonName
lateinit var loginId: String
var preferredLanguage: PreferredLanguage? = null
}
Here, preferredLanguage is an enum, so it can be included with the @Enumerated annotation within the entity:
@Entity
class Person : Identity...