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 }
  • 相关阅读:
    合并两个排序的链表
    反转链表
    java网络编程之TCP通讯
    java网络编程之UDP通讯
    Java中的线程同步机制
    阿里研发工程师面试题三个小结
    Android开发的进阶之路
    获取一个字符串中每一个字母出现的次数使用map集合
    Android常见面试题目
    Java垃圾回收
  • 原文地址:https://www.cnblogs.com/Answer1215/p/13945906.html
Copyright © 2011-2022 走看看