【发布时间】:2018-01-07 04:48:37
【问题描述】:
我有一个类似的问题如下,但解决方案没有解决我的问题。
hibernate composite Primary key contains a composite foreign key, how to map this
我正在尝试加入 2 个表,每个表都有一个带有部分外键引用的复合主键。
Table A
--------
f1 (pk)
f2 (pk)
f3 (pk)
f4 (pk)
Table B
--------
f1 (pk, fk)
f2 (pk, fk)
f5 (pk)
f6 (pk)
I created A, APK, B, BPK
在 A 中:
private Set<B> bSet;
@OneToMany(targetEntity=B.class, cascade = CascadeType.ALL, mappedBy= "bpk.a")
public Set<MovesEntity> getBSet() {
return bSet;
}
在 BPK 中:
@ManyToOne(fetch=FetchType.EAGER)
@JoinColumns({
@JoinColumn(name="f1", referencedColumnName="f1", nullable=false, insertable=false, updatable = false),
@JoinColumn(name="f2", referencedColumnName="f2", nullable=false, insertable=false, updatable = false)
})
public A getA() {
return a;
}
上述方法给了我这个例外:
AnnotationException: referencedColumnNames(f1, f2) of entity.BPK.bpk.a
referencing com.example.entity.A not mapped to a single property
你能帮忙吗?
【问题讨论】:
-
JPA 不允许部分主键。 B 需要 A 的每个主键字段的外键来唯一标识 A。如果只有 f1 和 f2 唯一标识 A,那么它们应该是它的主键(而不是 f2 和 f3)。
-
非常感谢克里斯。这解决了我的问题!.. 在这种情况下,我必须在 APK 中将 f3 和 f4 注释为 @Basic 而不是 id 或 embeddedId。我只将此服务用于 SELECT,所以我想它可以吗?
-
f3 和 f4 不应在 APK 中,而应在 A 中作为基础。
-
是的,作为 A 中的 Basic - 谢谢!
标签: java hibernate jpa foreign-keys composite-key