我试图实现一个奇怪的数据库设计,但我无法弄清楚如何去做。在两个实体之间存在多对一关系,两个实体都具有组合的主键。外键由另一个权限的主键中存在的字段和不在主键中的字段组成。希望有人能帮助我。
@Entity
@Table(name = "A")
public class A {
@EmbeddedId
private Aid id;
@JoinColumn(name = "FOREIGN_KEY_FIELD2", referencedColumnName = "foreignKeyField2")
private B foreignKeyField2;
}
@Embeddable
public class Aid{
@JoinColumn(name = "FOREIGN_KEY_FIELD1", referencedColumnName = "foreignKeyField1")
private B foreignKeyField1;
private String otherField;
}
@Entity
@Table(name = "B")
public class B {
@EmbeddedId
private Bid id;
@OneToMany(targetEntity = A.class, fetch = FetchType.LAZY)
@JoinColumns({ @JoinColumn(name = "FOREIGN_KEY_FIELD1", referencedColumnName = "foreignKeyField1"),
@JoinColumn(name = "FOREIGN_KEY_FIELD2", referencedColumnName = "foreignKeyField2") })
private List<A> as;
}
@Embeddable
public class Bid {
private String foreignKeyField1;
private String otherField;
}
祝你好运。 提前谢谢。
答案 0 :(得分:0)
使用@EmbeddedId无法解决此问题,您必须定义一个IdClass。
以下是一个例子:
@Entity @IdClass(PartPK.class)
public class Part {
@Id int partNo;
@Id @ManyToOne
Supplier supplier;
}
public class PartPK {
int partNo;
int supplier;
}