简体   繁体   English

使用休眠和 3 个实体设置表

[英]Setting up table with hibernate and 3 entities

I am new with Hibernate and have a question about setting up my database.我是 Hibernate 的新手,有一个关于设置我的数据库的问题。

I have 3 entities: Project, R1 and R2.我有 3 个实体:Project、R1 和 R2。 A Project can have one to many R1 and R1 can belong to more than 1 project.一个项目可以有一对多的 R1,而 R1 可以属于多个项目。 R2 belongs to a couple of Project-R1. R2 属于几个 Project-R1。 And R2 can belong to multiple couples of Project-R1.而 R2 可以属于多对 Project-R1。

Examples:例子:

My idea is to first couple Project to R1 and let Project remember which R1's it has.我的想法是首先将 Project 与 R1 结合起来,让 Project 记住它有哪些 R1。 So a third table for joining with the Primary key of Project and R1.所以第三个表用于连接 Project 和 R1 的主键。

When I have to couple R2 to a Project-R1 couple I was thinking of using another join table with the Primary keys of Project-R1 and R2.当我必须将 R2 与 Project-R1 配对时,我想使用另一个带有 Project-R1 和 R2 主键的连接表。 But how can i make the last connection using Hibernate without making a join table that has its own Primary key next to Project-R1-R2.但是,如何使用 Hibernate 进行最后一次连接而不创建在 Project-R1-R2 旁边有自己的主键的连接表。

is it even possible?甚至有可能吗?

Hibernate docs give this example for ternary associations: Hibernate 文档给出了三元关联的这个例子:

@Entity
public class Company {
 @Id 
 int id;
 ...
 @OneToMany // unidirectional
 @MapKeyJoinColumn(name="employee_id")
 Map<Employee, Contract> contracts;
}

// or

<map name="contracts">
 <key column="employer_id" not-null="true"/>
 <map-key-many-to-many column="employee_id" class="Employee"/>
 <one-to-many class="Contract"/>
</map>

The docs also note that R2-(P+R1) type of association would typically be modeled as a separate entity class instead.文档还指出,R2-(P+R1) 类型的关联通常会被建模为单独的实体类。 It would also be my approach for this.这也是我的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM