[Kotlin] Mapping between two entities

For example we have tow Entities:

package com.virtualpairprogrammers.theater.domain

import javax.persistence.*

@Entity
data class Performance(
        @Id @GeneratedValue(strategy = GenerationType.AUTO)
        val id: Long,
        val title: String,
        @OneToMany(mappedBy = "performance")
        val bookings: List<Booking>
)
package com.virtualpairprogrammers.theater.domain

import javax.persistence.*

@Entity
data class Booking(
        @Id @GeneratedValue(strategy = GenerationType.AUTO)
        val id: Long,
        // relationship
        // multi seats can exists in one booking
        @ManyToOne
        val seat: Seat,
        @ManyToOne
        val performance: Performance,
        val customerName: String
)

 

Actually, it is not good to put 'bookings, performance, customerName' into constructor. Because it will generate very long sql string.

A better way:

package com.virtualpairprogrammers.theater.domain

import javax.persistence.*

@Entity
data class Performance(
        @Id @GeneratedValue(strategy = GenerationType.AUTO)
        val id: Long,
        val title: String
) {
        @OneToMany(mappedBy = "performance")
        lateinit var bookings: List<Booking>
}



package com.virtualpairprogrammers.theater.domain import javax.persistence.* @Entity data class Booking( @Id @GeneratedValue(strategy = GenerationType.AUTO) val id: Long, val customerName: String ) { // relationship // multi seats can exists in one booking @ManyToOne lateinit var seat: Seat @ManyToOne lateinit var performance: Performance }

 

posted @ 2020-11-08 21:18  Zhentiw  阅读(119)  评论(0)    收藏  举报