单向一对多,父对象具有复合键

时间:2017-01-12 20:47:39

标签: java database spring hibernate jpa

我需要在父和子之间创建一对多的映射。 Parent具有嵌入式ID。我采用了这种方法,我得到了:

修改 父表包含3列,这些列是来自3个不同子表的外键。

org.hibernate.AnnotationException: A Foreign key refering com.Parent from com.Child has the
wrong number of column. should be 3
?- bl(?t and ?X, R).
X = R, R = f ;
X = R, R = t.

?- bl(?t or ?X, R).
R = t.

?- bl(?A or (?B and ?C), R).
A = B, B = R, R = f ;
A = C, C = R, R = f,
B = t ;
A = f,
B = C, C = R, R = t ;
A = R, R = t.

?- table(?A or (?B and ?A), R).
R = [f-[f, f], f-[f, t], t-[t, _7734]].

第二种方法:

使用以下映射也会给出相同的上述异常:

Parent {
    @EmbeddedId
    private ParentEmbeddedId id;

    @OneToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "Child",
               joinColumns = {
                   @JoinColumn(name="childId2"),
                   @JoinColumn(name="childId3")
               },
               inverseJoinColumns={
                   @JoinColumn(name="childId1")
               })
    private Collection<Child> children;

}

ParentEmbeddedId {
    private long childId1;
    private long childId2;
    private long childId3;
}

Child {
    private long childId1;
}

第3种方法:

@OneToMany
@JoinColumn(name = "childId1")
private Collection<Child> children;

这会导致

@OneToMany
@JoinColumns({
    @JoinColumn(name="childId1", referencedColumnName="childId1"),
    @JoinColumn(name="childId2", referencedColumnName="childId2"),
    @JoinColumn(name="childId3", referencedColumnName="childId3"),
})
private Collection<Child> Children

基本上,hibernate生成的查询试图使用“Child”表中不存在的“childId3”列来运行查询。

1 个答案:

答案 0 :(得分:-1)

尝试

 Child {
    private long childId1;
    private long childId2;
    private long childId3;
}