I’ve got three tables in my database:
create table A (
a_id int primary key
);
create table B (
b_id int primary key references A(a_id)
);
create table C (
c_id int primary key,
b_id int references B(b_id)
);
and I modelled them using following JPA-annotated classes:
@Entity(name = "A")
@Table(name = "A")
public class A {
@SuppressWarnings("unused")
@Id
@Column(name = "A_ID")
private int id;
}
@Entity(name = "B")
@Table(name = "B")
@PrimaryKeyJoinColumns({@PrimaryKeyJoinColumn(name = "B_ID", referencedColumnName = "A_ID")})
public class B extends A {
}
@Entity(name = "C")
@Table(name = "C")
public class C {
@SuppressWarnings("unused")
@Id
@Column(name = "C_ID")
private int id;
@SuppressWarnings("unused")
@OneToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH, CascadeType.DETACH})
@JoinColumns(value = {
@JoinColumn(name = "B_ID", referencedColumnName = "B_ID", insertable = false, updatable = false)})
private B b;
}
When I try to use these classes with Hibernate entity manager, it complains with the following error:
Unable to find column with logical name: B_ID in org.hibernate.mapping.Table(A) and its related supertables and secondary tables
Changing referencedColumnName ont the C to B join to A_ID seems to make it happy. Why is that? Can’t I model the relationships as they are in the database?
What was missing was a specification of inheritance strategy:
The JPA default is
SINGLE_TABLE, which collapses entire class hierarchy into a single table.