如果我也将该列提升为 PK(也称为识别关系),会发生什么情况?由于该列现在是 PK,因此我必须使用 @Id (...) 对其进行标记。
这增强了对派生标识符实际上是JPA 2.0 中的新内容 https://stackoverflow.com/questions/2435253/a-concise-clear-list-of-what-is-new-in-jpa2/2440516#2440516(参见章节2.4.1 派生身份对应的主键在 JPA 2.0 规范中),JPA 1.0 不允许Id
on a OneToOne
or ManyToOne
。使用 JPA 1.0,您必须使用PrimaryKeyJoinColumn
并且还定义一个Basic
Id
外键列的映射。
现在的问题是:@Id + @JoinColumn 与 @PrimaryKeyJoinColumn 相同吗?
您可以获得类似的结果,但使用Id
on OneToOne
or ManyToOne
is 简单得多是使用 JPA 2.0 映射派生标识符的首选方法。PrimaryKeyJoinColumn
可能仍会用于JOINED继承策略。 JPA 2.0 规范的相关部分如下:
11.1.40 PrimaryKeyJoinColumn注解
The PrimaryKeyJoinColumn
注解
指定一个主键列
用作连接到的外键
另一张桌子。
The PrimaryKeyJoinColumn
annotation
is used to join the primary table of
an entity subclass in the JOINED
mapping strategy to the primary table
of its superclass; it is used within a
SecondaryTable
annotation to join a
secondary table to a primary table;
and it may be used in a OneToOne
mapping in which the primary key of
the referencing entity is used as a
foreign key to the referenced
entity[108].
...
If no PrimaryKeyJoinColumn
注解是为子类指定的
在 JOINED 映射策略中,
假设外键列
与主键同名
主表的列
超类。
...
Example:客户和尊贵客户子类
@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
PrimaryKeyJoinColumn
for the
OneToOne mapping case.
See also
- 通过一对一关系的主键 http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Primary_Keys_through_OneToOne_and_ManyToOne_Relationships
这个来源http://weblogs.java.net/blog/felipegaucho/archive/2009/10/24/jpa-join-table-additional-state http://weblogs.java.net/blog/felipegaucho/archive/2009/10/24/jpa-join-table-additional-state声明使用 @ManyToOne 和 @Id 适用于 JPA 1.x。现在谁是正确的?
作者使用的是预发行版JPA 2.0EclipseLink 的兼容版本(版本2.0.0-M7 http://kenai.com/projects/puj/sources/arena/revision/188在撰写本文时)写一篇有关 JPA 1.0(!)的文章。这篇文章有误导性,作者使用的是NOTJPA 1.0 的一部分。
作为记录,支持Id
on OneToOne
and ManyToOne
已添加到 EclipseLink 1.1 中(请参阅 from 詹姆斯·萨瑟兰 http://wiki.eclipse.org/User:James.sutherland.oracle.com, EclipseLink 的提交者和主要贡献者Java持久化 http://en.wikibooks.org/wiki/Java_Persistence维基百科书)。但我要强调的是,这是NOTJPA 1.0 的一部分。