Two tables like:
CREATE TABLE foo (
id INT PRIMARY KEY,
x TEXT
);
CREATE TABLE bar (
foo_id INT REFERENCES foo (id) ON DELETE CASCADE,
y TEXT,
z TEXT
);
...can be mapped like so:
@Table(name = "foo")
@SecondaryTable(name = "bar", pkJoinColumns = @PrimaryKeyJoinColumn(name = "foo_id", referencedColumnName = "id"))
class Foo {
@Id
int id;
@Embedded
@AttributeOverrides({
@AttributeOverride(name = "y", column = @Column(table = "bar")),
@AttributeOverride(name = "z", column = @Column(table = "bar"))
})
Bar bar;
}
@Embeddable
class Bar {
String y;
String z;
}
Is there a less awkward way to do this mapping, using either just standard JPA annotations, or else Hibernate-specific annotations (and without introducing a parent reference in the embeddable object)?
Compare this to how easily a collection of @Embeddable
objects can be referenced using an @ElementCollection
and @CollectionTable
.