简体   繁体   English

单向一对多,父级具有复合密钥

[英]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.

相关问题 复合键和多对一 - Composite key and many to one 具有复合子项键的单向OneToMany,不传播父级ID - Unidirectional OneToMany with composite children key, not propagating parent id 我如何在Hibernate中映射多对一关联,其中孩子有一个组合键,其中一部分是父级的主键? - How do I map a many-to-one association in Hibernate where the child has a composite key and a part of that is the primary key in the parent? Hibernate批注使用复合主键一对一映射单向关联 - Hibernate annotations to map one to one unidirectional association with composite primary key 使用复合键的Hibernate单向OneToMany - Hibernate Unidirectional OneToMany with composite key 具有一对多休眠状态的复合键 - Composite key with one to many hibernate 一对多单向父子ID级联保存 - One-to-Many Unidirectional Parent-Child ID Cascade Save 休眠一对多映射,其中一类在多侧具有复合主键 - Hibernate one to many mapping where one class has a composite primary key on many side Spring / JPA保持一对多的子组合键,其父键为null - Spring/JPA Persisting One-To-Many, Child Composite Key with key from parent is null 一对多单向关系的外键约束问题 - Foreign key constraint issue for One-to-Many unidirectional relation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM