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;
@Column(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.