Java JPA:@JoinColumn 和@PrimaryKeyJoinColumn 之间的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3417097/
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 23:08:21  来源:igfitidea点击:

JPA: difference between @JoinColumn and @PrimaryKeyJoinColumn?

javahibernatejpajakarta-eeidentifying-relationship

提问by Kawu

What's the exact difference between @JoinColumnand @PrimaryKeyJoinColumn?

@JoinColumn和之间的确切区别是@PrimaryKeyJoinColumn什么?

You use @JoinColumnfor columns that are part of a foreign key. A typical column could look like (e.g. in a join table with additional attributes):

@JoinColumn用于作为外键一部分的列。典型的列可能看起来像(例如在具有附加属性的连接表中):

@ManyToOne
@JoinColumn(name = "...")
private OtherClass oc;

What happens if I promote the column to be a/the PK, too (a.k.a. identifying relationship)? As the column is now the PK, I must tag it with @Id:

如果我也将该列提升为 a/PK(又名识别关系),会发生什么情况?由于该列现在是 PK,我必须将其标记为@Id

@Id
@ManyToOne
@JoinColumn(name = "...")
private OtherClass oc;

Now the question is:

现在的问题是:

Are @Id+ @JoinColumnthe same as just @PrimaryKeyJoinColumn?:

@Id+@JoinColumn上面一样@PrimaryKeyJoinColumn

@ManyToOne
@PrimaryKeyJoinColumn(name = "...")
private OtherClass oc;

If not, what's @PrimaryKeyJoinColumnthere for?

如果不是,那@PrimaryKeyJoinColumn有什么用?

采纳答案by Pascal Thivent

What happens if I promote the column to be a/the PK, too (a.k.a. identifying relationship)? As the column is now the PK, I must tag it with @Id (...).

如果我也将该列提升为 a/PK(又名识别关系),会发生什么情况?由于该列现在是 PK,我必须用 @Id (...) 标记它。

This enhanced support of derived identifiersis actually part of the new stuff in JPA 2.0(see the section 2.4.1 Primary Keys Corresponding to Derived Identitiesin the JPA 2.0 specification), JPA 1.0 doesn't allow Idon a OneToOneor ManyToOne. With JPA 1.0, you'd have to use PrimaryKeyJoinColumnand also define a BasicIdmapping for the foreign key column.

这种对派生标识符的增强支持实际上是JPA 2.0 中新内容的一部分(请参阅JPA 2.0 规范中的2.4.1派生标识符对应的主键部分),JPA 1.0 不允许Id使用OneToOneManyToOne。使用 JPA 1.0,您必须使用PrimaryKeyJoinColumn并定义BasicId外键列的映射。

Now the question is: are @Id + @JoinColumn the same as just @PrimaryKeyJoinColumn?

现在的问题是:@Id + @JoinColumn 和 @PrimaryKeyJoinColumn 一样吗?

You can obtain a similar result but using an Idon OneToOneor ManyToOneis much simplerand is the preferred way to map derived identifiers with JPA 2.0. PrimaryKeyJoinColumnmight still be used in a JOINEDinheritance strategy. Below the relevant section from the JPA 2.0 specification:

可以得到类似的结果,但使用IdOneToOneManyToOne简单得多,并且是衍生识别符与JPA 2.0映射的首选方式。PrimaryKeyJoinColumn可能仍会在JOINED继承策略中使用。在 JPA 2.0 规范的相关部分下方:

11.1.40 PrimaryKeyJoinColumn Annotation

The PrimaryKeyJoinColumnannotation specifies a primary key column that is used as a foreign key to join to another table.

The PrimaryKeyJoinColumnannotation is used to join the primary table of an entity subclass in the JOINEDmapping strategy to the primary table of its superclass; it is used within a SecondaryTableannotation to join a secondary table to a primary table; and it may be used in a OneToOnemapping in which the primary key of the referencing entity is used as a foreign key to the referenced entity[108].

...

If no PrimaryKeyJoinColumnannotation is specified for a subclass in the JOINED mapping strategy, the foreign key columns are assumed to have the same names as the primary key columns of the primary table of the superclass.

...

Example:Customer and ValuedCustomer subclass

@Entity
@Table(name="CUST")
@Inheritance(strategy=JOINED)
@DiscriminatorValue("CUST")
public class Customer { ... }

@Entity
@Table(name="VCUST")
@DiscriminatorValue("VCUST")
@PrimaryKeyJoinColumn(name="CUST_ID")
public class ValuedCustomer extends Customer { ... }

[108] The derived id mechanisms described in section 2.4.1.1 are now to be preferred over PrimaryKeyJoinColumnfor the OneToOne mapping case.

11.1.40 PrimaryKeyJoinColumn 注解

PrimaryKeyJoinColumn批注指定用作外键加入到另一个表的主键列。

PrimaryKeyJoinColumn注释用于连接在一个实体子类的主表JOINED映射策略到它的父类的主表; 它在 SecondaryTable注释中用于将辅助表连接到主表;并且它可以用在OneToOne映射中,其中引用实体的主键用作被引用实体的外键[108]

...

如果PrimaryKeyJoinColumn在 JOINED 映射策略中没有为子类指定注解,则假定外键列与超类的主表的主键列具有相同的名称。

...

示例:Customer 和 ValuedCustomer 子类

@Entity
@Table(name="CUST")
@Inheritance(strategy=JOINED)
@DiscriminatorValue("CUST")
public class Customer { ... }

@Entity
@Table(name="VCUST")
@DiscriminatorValue("VCUST")
@PrimaryKeyJoinColumn(name="CUST_ID")
public class ValuedCustomer extends Customer { ... }

[108]第 2.4.1.1 节中描述的派生 id 机制现在优先 PrimaryKeyJoinColumn于 OneToOne 映射情况。

See also

也可以看看



This source http://weblogs.java.net/blog/felipegaucho/archive/2009/10/24/jpa-join-table-additional-statestates that using @ManyToOne and @Id works with JPA 1.x. Who's correct now?

此来源http://weblogs.java.net/blog/felipegaucho/archive/2009/10/24/jpa-join-table-additional-state指出使用 @ManyToOne 和 @Id 可与 JPA 1.x 配合使用。现在谁是对的?

The author is using a pre release JPA 2.0compliant version of EclipseLink (version 2.0.0-M7at the time of the article) to write an article about JPA 1.0(!). This article is misleading, the author is using something that is NOTpart of JPA 1.0.

作者正在使用EclipseLink的预发布JPA 2.0兼容版本(本文时为2.0.0-M7版)来写一篇关于 JPA 1.0(!)的文章。这篇文章具有误导性,作者使用的东西不是JPA 1.0 的一部分。

For the record, support of Idon OneToOneand ManyToOnehas been added in EclipseLink 1.1 (see this messagefrom James Sutherland, EclipseLink comitter and main contributor of the Java Persistencewiki book). But let me insist, this is NOTpart of JPA 1.0.

作为记录,在 EclipseLink 1.1 中添加了对IdonOneToOne和 的支持ManyToOne(请参阅来自James Sutherland 的消息,EclipseLink 提交者和Java Persistencewiki 书籍的主要贡献者)。但让我坚持,这不是JPA 1.0 的一部分。

回答by GMsoF

I normally differentiate these two via this diagram:

我通常通过这张图区分这两者:

Use PrimaryKeyJoinColumn

PrimaryKeyJoinColumn

enter image description here

enter image description here

Use JoinColumn

JoinColumn

enter image description here

enter image description here

回答by Philip Read

I know this is an old post, but a good time to use PrimaryKeyColumnwould be if you wanted a unidirectional relationship or had multiple tables all sharing the same id.

我知道这是一篇旧帖子,但PrimaryKeyColumn如果您想要单向关系或有多个表共享相同的 ID,那么使用的好时机是。

In general this is a bad idea and it would be better to use foreign key relationships with JoinColumn.

一般来说,这是一个坏主意,最好将外键关系与JoinColumn.

Having said that, if you are working on an older database that used a system like this then that would be a good time to use it.

话虽如此,如果您正在处理使用此类系统的旧数据库,那么这将是使用它的好时机。