I have found two solutions to this problem.
The first one is rather a workaround and is not so neat as the second one.
Define the primary key of the B
entity as composite key containing col_a
, col_b
, and col_c
and what was supposed to be the primary key in the first place, define as unique constraint. The disadvantage is that the column col_c
is not really conceptually a part of primary key.
@Entity
class A {
@Id
private int b;
@Id
private int c;
}
@Entity
@Table(uniqueConstraints = {@UniqueConstraint(columnNames = { "a", "b" }) })
class B {
@Id
private int a;
@Id
@ManyToOne(optional = false)
@JoinColumns(value = {
@JoinColumn(name = "b", referencedColumnName = "b"),
@JoinColumn(name = "c", referencedColumnName = "c") })
private A entityA;
}
The second uses @EmbeddedId
and @MapsId
annotations and does exactly what I wanted to be done at the very beginning.
@Entity
class A {
@Id
private int b;
@Id
private int c;
}
@Embeddable
class BKey {
private int a;
private int b;
}
@Entity
class B {
@EmbeddedId
private BKey primaryKey;
@MapsId("b")
@ManyToOne(optional = false)
@JoinColumns(value = {
@JoinColumn(name = "b", referencedColumnName = "b"),
@JoinColumn(name = "c", referencedColumnName = "c") })
private A entityA;
}