zoukankan      html  css  js  c++  java
  • [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 }
  • 相关阅读:
    219. Contains Duplicate II
    189. Rotate Array
    169. Majority Element
    122. Best Time to Buy and Sell Stock II
    121. Best Time to Buy and Sell Stock
    119. Pascal's Triangle II
    118. Pascal's Triangle
    88. Merge Sorted Array
    53. Maximum Subarray
    CodeForces 359D Pair of Numbers (暴力)
  • 原文地址:https://www.cnblogs.com/Answer1215/p/13945906.html
Copyright © 2011-2022 走看看