[英]Unidirectional one to many, parent has composite key
I need to create a one to many mapping between a parent and child. 我需要在父母与孩子之间创建一对多映射。 Parent has an embedded id.
父级具有嵌入式ID。 I took this approach and I get:
我采用了这种方法,并且得到:
EDIT: The parent table contains 3 columns, which are foreign keys from 3 different Child tables. 编辑:父表包含3列,它们是来自3个不同子表的外键。
org.hibernate.AnnotationException: A Foreign key refering com.Parent from com.Child has the wrong number of column. should be 3
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;
}
2nd approach: 第二种方法:
Using the below mapping also gives the same above exception: 使用下面的映射也会产生与上面相同的异常:
@OneToMany
@JoinColumn(name = "childId1")
private Collection<Child> children;
3rd approach: 第三种方法:
@OneToMany
@JoinColumns({
@JoinColumn(name="childId1", referencedColumnName="childId1"),
@JoinColumn(name="childId2", referencedColumnName="childId2"),
@JoinColumn(name="childId3", referencedColumnName="childId3"),
})
private Collection<Child> Children
This causes 这导致
java.sql.SQLSyntaxErrorException: ORA-00904: "Child2_"."childId3": invalid identifier;
Basically, the query generated by hibernate is trying to run query using "childId3" column that does not exist in "Child" table. 基本上,hibernate生成的查询试图使用“ Child”表中不存在的“ childId3”列运行查询。
Try with 试试看
Child {
private long childId1;
private long childId2;
private long childId3;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.