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 }
  • 相关阅读:
    软工课设第一周周五报告
    软工课设第一周周四报告
    软工课设第一周周三报告
    软工课设第一周周二报告
    软工课设第一周周一报告
    团队项目记录4
    团队项目记录3
    团队项目记录2
    jQuery 打气球小游戏 点击气球爆炸效果
    计网第二章:物理层
  • 原文地址:https://www.cnblogs.com/Answer1215/p/13945906.html
Copyright © 2011-2022 走看看