很好地设计了多对多关系

huangapple go评论75阅读模式
英文:

Does many-to-many relationship design properly

问题

我已经基于Hibernate创建了两个具有彼此之间多对多关系的表格。

这些表格:

@Entity
@Table(name = "Interests")
data class Interest(
    var description: String = "") : PanacheEntity()

@Entity
@Table(name = "Accounts")
data class Account(@field:Id var id: UUID? = null,
                   @ManyToOne(fetch = FetchType.LAZY) var gender: Gender? = null,
                   var birthday: Long = 0,
                   @ManyToMany(fetch = FetchType.LAZY, cascade = [CascadeType.PERSIST])
                   @JoinTable(
                       name = "user_interests",
                       joinColumns = [JoinColumn(name = "user_id")],
                       inverseJoinColumns = [JoinColumn(name = "interest_id")]
                   )
                   var interests: List<Interest> = listOf()) : PanacheEntityBase

在幕后,Hibernate会创建一个单独的表来保存外键。以下是生成的DDL:

create table user_interests
(
    user_id     uuid   not null
        constraint fkfxfdgodf1o1gxd8offdxrhcwu
            references accounts,
    interest_id bigint not null
        constraint fkdtbf68t9l8ehm46bi6ko45kb5
            references interests
);

问题是,constraint后面的隐秘字符串是什么意思,而且DDL的定义是否正确?

英文:

I have created two tables based on Hibernate that have many-to-many relationship to each other.

The tables:

@Entity
@Table(name = &quot;Interests&quot;)
data class Interest(
    var description: String = &quot;&quot;) : PanacheEntity()

@Entity
@Table(name = &quot;Accounts&quot;)
data class Account(@field:Id var id: UUID? = null,
                   @ManyToOne(fetch = FetchType.LAZY) var gender: Gender? = null,
                   var birthday: Long = 0,
                   @ManyToMany(fetch = FetchType.LAZY, cascade = [CascadeType.PERSIST])
                   @JoinTable(
                       name = &quot;user_interests&quot;,
                       joinColumns = [JoinColumn(name = &quot;user_id&quot;)],
                       inverseJoinColumns = [JoinColumn(name = &quot;interest_id&quot;)]
                   )
                   var interests: List&lt;Interest&gt; = listOf()) : PanacheEntityBase

Behind the scenes, Hibernate creates a separate table to hold the foreign keys. Here is the generated DDL:

create table user_interests
(
    user_id     uuid   not null
        constraint fkfxfdgodf1o1gxd8offdxrhcwu
            references accounts,
    interest_id bigint not null
        constraint fkdtbf68t9l8ehm46bi6ko45kb5
            references interests
);

The question is, what are the cryptic strings after the constraint and is the DDL correctly defined?

答案1

得分: 2

Interest 类中,还要声明一个 var accounts: List<Account>,并进行反向映射!

英文:

In Interest class, declare "var accounts: List<Account>" too, with inverse mapping!

huangapple
  • 本文由 发表于 2020年9月5日 04:57:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63747957.html
匿名

发表评论

匿名网友

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

确定