SQL一对多关系

huangapple 未分类评论48阅读模式
英文:

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 = &quot;tickets&quot;)
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 = &quot;customers&quot;)
data class Customer(
    @Id
    val id:Long,
    val email:String,
    val discordUserId:String,
    val amountSpent:Int,
    @OneToMany
    @JoinColumn(name = &quot;ticket_id&quot;, table = &quot;tickets&quot;)
    val tickets:Set&lt;Ticket&gt;
)

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 = &quot;customers&quot;)
data class Customer(
    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    var id:Long,
    var email:String,
    var discordUserId:String,
    var amountSpent:Int,
    @OneToMany(mappedBy = &quot;customer&quot;, cascade = [CascadeType.ALL], orphanRemoval = true, fetch = FetchType.EAGER)
    var tickets:Set&lt;Ticket&gt; = setOf()
)

@Entity
@Table(name = &quot;tickets&quot;)
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=&quot;customer_id&quot;, nullable=false)
    var customer: Customer? = null
)

huangapple
  • 本文由 发表于 2020年5月29日 12:26:42
  • 转载请务必保留本文链接:https://java.coder-hub.com/62078730.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定