英文:
SQL OneToMany Relationship
问题
我是你的中文翻译,以下是已经翻译好的部分:
我对SQL关系还不熟悉,我正在使用Spring,并且希望在客户和票务之间建立一对多的关系,例如客户可以拥有多个票务,但票务不能有多个客户。以下是我已经完成的部分:
@Entity
@Table(name = "tickets")
data class Ticket(
@Id
val id: Long,
val ticketId: String,
val price: Int,
val openDate: LocalDateTime,
val closeDate: LocalDateTime,
@ManyToOne
val customer: Customer
)
@Entity
@Table(name = "customers")
data class Customer(
@Id
val id: Long,
val email: String,
val discordUserId: String,
val amountSpent: Int,
@OneToMany
@JoinColumn(name = "ticket_id", table = "tickets")
val tickets: Set<Ticket>
)
然而,我在启动时遇到了错误:
> Caused by: org.hibernate.cfg.NotYetImplementedException: Collections having FK in secondary table
只是想知道如何解决这个问题,
英文:
Im new to SQL relations, Im using spring, and im wanting to make a OneToMany relationship between a customer and a ticket, eg Customer can have multiple tickets but ticket cant have multiple customers, heres what ive got,
@Entity
@Table(name = "tickets")
data class Ticket(
@Id
val id:Long,
val ticketId:String,
val price:Int,
val openDate:LocalDateTime,
val closeDate:LocalDateTime,
@ManyToOne
val customer:Customer
)
@Entity
@Table(name = "customers")
data class Customer(
@Id
val id:Long,
val email:String,
val discordUserId:String,
val amountSpent:Int,
@OneToMany
@JoinColumn(name = "ticket_id", table = "tickets")
val tickets:Set<Ticket>
)
However, I get an error on startup
> Caused by: org.hibernate.cfg.NotYetImplementedException: Collections having FK in secondary table
Just wondering how I can resolve this,
答案1
得分: 0
你应该在你的类之间实现双向关系,类似于这样:
@Entity
@Table(name = "customers")
data class Customer(
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
var id: Long,
var email: String,
var discordUserId: String,
var amountSpent: Int,
@OneToMany(mappedBy = "customer", cascade = [CascadeType.ALL], orphanRemoval = true, fetch = FetchType.EAGER)
var tickets: Set<Ticket> = setOf()
)
@Entity
@Table(name = "tickets")
data class Ticket(
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
var id: Long,
var ticketId: String,
var price: Int,
var openDate: LocalDateTime,
var closeDate: LocalDateTime,
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name="customer_id", nullable=false)
var customer: Customer? = null
)
英文:
You should implement bidirectional relationship between your classes like this:
@Entity
@Table(name = "customers")
data class Customer(
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
var id:Long,
var email:String,
var discordUserId:String,
var amountSpent:Int,
@OneToMany(mappedBy = "customer", cascade = [CascadeType.ALL], orphanRemoval = true, fetch = FetchType.EAGER)
var tickets:Set<Ticket> = setOf()
)
@Entity
@Table(name = "tickets")
data class Ticket(
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
var id:Long,
var ticketId:String,
var price:Int,
var openDate:LocalDateTime,
var closeDate:LocalDateTime,
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name="customer_id", nullable=false)
var customer: Customer? = null
)
专注分享java语言的经验与见解,让所有开发者获益!
评论