OneToMany mapping

Now, let's modify the Person entity to add other entities to it. Let's say that Person has a list of contacts, which is explained in the following code:

@Table
@Entity
class Person : Identity() {
@Id
lateinit var identifier: UUID
lateinit var name: PersonName
lateinit var loginId: String

@Enumerated(EnumType.STRING)
var preferredLanguage: PreferredLanguage? = null

lateinit var contact: List<ContactDetails>

}

ContactDetails is another type here:

class ContactDetails {
private lateinit var id: UUID
lateinit var number: String

private var type: ContactType? = null
}

Let's say ContactDetails has a ContactType, which is an enum type. A contact type might be, for example, WORK, MOBILE, or HOME:

enum class ContactType(val contactType: String) {
WORK("work"), MOBILE("mobile"), HOME("home")
}

The Person type instance can now have multiple contact details. This has to be mapped to the database, which means that ContactDetails is also an entity and therefore has to be annotated with @Entity:

@Entity
class ContactDetails {
@Id
private lateinit var id: UUID
private lateinit var number: String

@Enumerated(EnumType.STRING)
private var type: ContactType? = null
}

Let's now say that the Person entity also has an Address:

class Address {
lateinit var street: String
lateinit var city: String
lateinit var state: String
lateinit var country: String
}

Now, the Person entity class appears as follows:

class Person : Identity() {
@Id
lateinit var identifier: UUID
lateinit var name: PersonName
lateinit var loginId: String

@Enumerated(EnumType.STRING)
var preferredLanguage: PreferredLanguage? = null

lateinit var contact: List<ContactDetails>
lateinit var address: Address

}

The interesting thing here is that Address is not identifiable, as we have not marked it with the @Entity annotation. ContactType, on the other hand, is identifiable; it maps to the contact_type table and it has to have an ID. We are also going to map the enum as a string and then map all the values in our domain. Let's take a look at how we can do this.

The  Person class includes a list of contacts that maps to a table called contacts in the database. Since we have two tables in our database, person and contacts, they have to be joined with a link.

We need to specify the JPA annotation @OneToMany to bind these two entities. Consider the following code:

@Entity
class Person : Identity() {
@Id
lateinit var identifier: UUID
lateinit var name: PersonName
lateinit var loginId: String

@Enumerated(EnumType.STRING)
var preferredLanguage: PreferredLanguage? = null

@OneToMany
lateinit var contact: List<ContactDetails>
lateinit var address: Address
}

The reason that we choose the @OneToMany annotation is that we have one person entity that can have multiple contact details. 

JPA models these entities to the appropriate tables in the database.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset