英文:
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 = "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
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!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论