Java @OneToMany 没有反向关系也没有连接表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2095998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:29:54  来源:igfitidea点击:

@OneToMany without inverse relationship and without a join table?

javahibernateormjpaone-to-many

提问by jsight

This is a similar problem to "Hibernate @OneToMany without a separate join table", in that I need a @OneToMany relationship without a join table. However, I would also like to not define the inverse relationship. Removing the inverse seems to result in a join table being automatically generated... is there a workaround for this?

这与“没有单独的连接表的 Hibernate @OneToMany”类似,因为我需要一个没有连接表的 @OneToMany 关系。但是,我也不想定义逆关系。删除逆似乎会导致自动生成连接表......是否有解决方法?

采纳答案by Arthur Ronald

In JPA 2.0+ you can use @JoinColumn as a way to avoid to generate joined table.

在 JPA 2.0+ 中,您可以使用 @JoinColumn 作为避免生成连接表的方法。

Try it.

尝试一下。

@OneToMany
@JoinColumn(name="COLUMN_NAME")

UPDATE

更新

The info provided above has been extracted from EJB 3.0 o'reilly book (Look for The @JoinColumn annotation references the CUSTOMER_ID column in the PHONE table). However, plain JPA 1.0 specification does not support this feature. What it says is

上面提供的信息是从 EJB 3.0 o'reilly book 中提取的(查找 @JoinColumn 注释引用了 PHONE 表中的 CUSTOMER_ID 列)。但是,普通的 JPA 1.0 规范不支持此功能。它说的是

Unidirectional one-to-many relationships may be implemented using one-to-many foreign key mappings, however, such support is not required in this release. Applications that want to use a foreign key mapping strategy for one-to-many relationships should make these relationships bidirectional to ensure portability

可以使用一对多外键映射来实现单向一对多关系,但是,此版本中不需要此类支持。想要对一对多关系使用外键映射策略的应用程序应该使这些关系双向以确保可移植性

So in 1.0 it is a vendor-specific implementation(And it makes sense, The author works at JBoss - The red hat divison behind the hibernate)

因此,在 1.0 中,它是特定于供应商的实现(这是有道理的,作者在 JBoss 工作 - 休眠背后的红帽部门)

But it is supported by JPA 2.0 implementation

它由 JPA 2.0 实现支持

If the join is for a unidirectional OneToMany mapping using a foreign key mapping strategy, the foreign key is in the table of the target entity.

如果联接用于使用外键映射策略的单向 OneToMany 映射,则外键在目标实体的表中

回答by Pascal Thivent

The JPA 1.0 specification does NOTsupport unidirectional OneToMany mapping without a Join Table.

在JPA 1.0规范并NOT支持单向一对多映射没有加入表

And using a JoinColumnon a OneToManyisn't allowed in standardJPA 1.0 (only on a OneToOne, ManyToOneor ManyToMany). It is in JPA 2.0 though.

并使用JoinColumn上一个OneToMany在不允许标准JPA 1.0(仅在一个OneToOneManyToOneManyToMany)。不过它在 JPA 2.0 中。

From the JPA 1.0 specification:

从 JPA 1.0 规范:

2.1.8.5.1 Unidirectional OneToMany Relationships

The following mapping defaults apply:

Entity A is mapped to a table named A. Entity B is mapped to a table named B. There is a join table that is named A_B(owner name first). This join table has two foreign key columns. One foreign key column refers to table Aand has the same type as the primary key of table A. The name of this foreign key column is formed as the concatenation of the following: the name of entity A; "_"; the name of the primary key column in table A. The other foreign key column refers to table B and has the same type as the primary key of table Band there is a unique key constraint on it. The name of this foreign key column is formed as the concatenation of the following: the name of the relationship property or field of entity A; "_"; the name of the primary key column in table B.

2.1.8.5.1 单向一对多关系

以下映射默认值适用:

实体 A 映射到名为 的表 A。实体 B 映射到名为 的表B。有一个已命名的连接表A_B(所有者名称在前)。这个连接表有两个外键列。一个外键列引用 tableA并且与 table的主键具有相同的类型 A。该外键列的名称由以下内容串联而成:实体 A 的名称;"_"; 表中主键列的名称A。另一个外键列引用表B,与表的主键类型相同B并且有一个唯一的键约束。该外键列的名称由以下内容串联而成:实体 A 的关系属性或字段的名称;"_"; 表中主键列的名称B

To sum up, if you don't want a Join Table (and full read/write support) and still want to be JPA compliant, make the association bidirectional (with an inverseside).

总而言之,如果您不想要连接表(和完整的读/写支持)并且仍然希望符合 JPA,请使关联双向(有inverse边)。

The wiki book link below discusses a trick (mapping the target table as the join table) to "work around" the problem but this only works for reads, writes won't work.

下面的维基书籍链接讨论了一个技巧(将目标表映射为连接表)来“解决”这个问题,但这仅适用于读取,写入不起作用。

References

参考

回答by dinesh ranawat

If there is no join table in database, then the relationship between two tables in database would be achieved by foreign key referring to primary key. If the relationship is through PK/FK, there has to be a property in the target class that refers back to the source so that the FK column is populated with a value. This property in the target class could be an id or a source object. If it is a source object then you need to have an inverse @ManyToOne in target class.

如果数据库中没有连接表,那么数据库中两个表之间的关系将通过外键引用主键来实现。如果关系是通过 PK/FK,则目标类中必须有一个属性来引用回源,以便用值填充 FK 列。目标类中的此属性可以是 id 或源对象。如果它是一个源对象,那么你需要在目标类中有一个反@ManyToOne。