[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 @   Zhentiw  阅读(114)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2018-11-08 [Node.js] Trigger a File Download in Express
2018-11-08 [WASM Rust] Create and Publish a NPM Package Containing Rust Generated WebAssembly using wasm-pack
2017-11-08 [TypeScript] Restrict null and undefined via Non-Nullable-Types in TypeScript
2017-11-08 [PWA] Deal with caches in PWA
2016-11-08 [AngularJS] Using an AngularJS directive to hide the keyboard on submit
2016-11-08 [Javascript Natural] Break up language strings into parts using Natural
2014-11-08 [AngularJS] Angular 1.3 Anuglar hint
点击右上角即可分享
微信分享提示