I have two classes Foo
and Bar
mapped to two different tables, and I'd like them to use the JOINED
inheritance strategy, but joined using two non-keyed columns. The schema is pretty weird, but I'm stuck with it. Here's my setup:
@Entity
@Table(name="foo")
@Inheritance(strategy=InheritanceType.JOINED)
public class Foo {
@Id
private Integer uniqueFooId;
@Column(name="column1")
private String column1;
@Column(name="column2")
private String column2;
@Col开发者_如何学Cumn(name="someValue")
private String someValue;
}
@Entity
@Table(name="bar")
public class Bar extends Foo {
@Id
private Integer uniqueBarId;
@Column(name="column1")
private String column1;
@Column(name="column2")
private String column2;
@Column(name="someOtherValue")
private String someOtherValue;
}
I'm not sure how @Inheritance decides what column to use to join against, but I'm assuming by default it uses the primary key(s). I'd like to join them against not just one column other than the primary key but two, in this case column1
and column2
.
I might even be going about this the wrong way. I'd appreciate any help or suggestions. Thanks!
JPA only allows multiple tables or inherited tables to be joined by the Id columns. What JPA provider are you using? Some provide other options.
See, http://en.wikibooks.org/wiki/Java_Persistence/Tables#Multiple_tables_with_foreign_keys
If using EclipseLink you can define any type of join you desire using a DescriptorCustomizer and addForeignKeyFieldNameForMultipleTable().
Otherwise, if you can't change the schema, you could try creating a view that does the join, and use TABLE_PER_CLASS inheritance and map the subclass to the view.
I'm not sure how @Inheritance decides what column to use to join against, but I'm assuming by default it uses the primary key(s).
This is correct and this is explained in the following section of the JPA 1.0 specification
9.1.32 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 aOneToOne
mapping in which the primary key of the referencing entity is used as a foreign key to the referenced entity....
If no
PrimaryKeyJoinColumn
annotation is specified for a subclass in theJOINED
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....
And the above annotation (or PrimaryKeyJoinColumns
when using composite keys) gives you some control on the name of the primary key column of the current table and the name of the primary key column of the table being joined to.
I'd like to join them against not just one column other than the primary key but two, in this case column1 and column2.
I'm not sure this will work but I'd try the following (in the subclass):
@Entity
@Table(name="bar")
@PrimaryKeyJoinColumns({
@PrimaryKeyJoinColumn(name="column1",
referencedColumnName="column1"),
@PrimaryKeyJoinColumn(name="column2",
referencedColumnName="column2")
})
public class Bar extends Foo {
@Id
private Integer uniqueBarId;
@Column(name="column1")
private String column1;
@Column(name="column2")
private String column2;
@Column(name="someOtherValue")
private String someOtherValue;
}
I wonder if the JPA provider will complain or not.
References
- JPA 1.0 Specification
- Section 9.1.32 "PrimaryKeyJoinColumn Annotation"
- Section 9.1.33 "PrimaryKeyJoinColumns Annotation"
精彩评论