JPA:@JoinColumn 和@PrimaryKeyJoinColumn 的区别?

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

对于属于外键的列,可以使用 @JoinColumn。典型的列可能类似(例如,在具有附加属性的联接表中) :

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

如果我把这个专栏也推广为 PK (又称识别关系) ,会发生什么?由于该列现在是 PK,我必须用 @Id标记它:

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

现在的问题是:

@Id + @JoinColumn@PrimaryKeyJoinColumn是一样的吗? :

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

如果没有,那 @PrimaryKeyJoinColumn是干什么的?

119860 次浏览

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 (...).

This enhanced support of derived identifiers is actually part of the new stuff in JPA 2.0 (see the section 2.4.1 Primary Keys Corresponding to Derived Identities in the JPA 2.0 specification), JPA 1.0 doesn't allow Id on a OneToOne or ManyToOne. With JPA 1.0, you'd have to use PrimaryKeyJoinColumn and also define a Basic Id mapping for the foreign key column.

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

You can obtain a similar result but using an Id on OneToOne or ManyToOne is much simpler and is the preferred way to map derived identifiers with JPA 2.0. PrimaryKeyJoinColumn might still be used in a JOINED inheritance strategy. Below the relevant section from the JPA 2.0 specification:

11.1.40 PrimaryKeyJoinColumn Annotation

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

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 annotation 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 PrimaryKeyJoinColumn for the OneToOne mapping case.

See also


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

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

For the record, support of Id on OneToOne and ManyToOne has been added in EclipseLink 1.1 (see this message from James Sutherland, EclipseLink comitter and main contributor of the Java Persistence wiki book). But let me insist, this is NOT part of JPA 1.0.

I normally differentiate these two via this diagram:

Use PrimaryKeyJoinColumn

enter image description here

Use JoinColumn

enter image description here

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

In general this is a bad idea and it would be better to use foreign key relationships with 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.

You use @JoinColumn when you want to manage (change the column name, set nullable and so on) the foreign key column in the target entity table. Here, the Address table will contains User table id like foreign key but the column it's will be name user_id (the second scheme of @Sam YC)

@Entity
public class Address implements Serializable {


@Id
@GeneratedValue
private String id;


private String city;


@OneToOne(optional = false)
@JoinColumn(name = "user_id", updatable = false)
private User user;
}

You use @PrimaryKeyJoinColumn, when you want to use the primary key of the referencing entity like the target entity primary key. Here the Address know the referencing User but Address table hasn't foreign key column, because it's has the same id than User Id (the first scheme of @Sam YC)

@Entity
public class Address implements Serializable {


@Id
@GeneratedValue(generator = "foreignKeyGenerator")
@GenericGenerator(
name = "foreignKeyGenerator",
strategy = "foreign",
parameters = @org.hibernate.annotations.Parameter(
name = "property", value = "userT"
)
)
private String id;
private String city;


@OneToOne(optional = false)
@PrimaryKeyJoinColumn
private User userT;
}