简体   繁体   English

映射到子表两次 - @OneToMany和@ManyToOne

[英]Mapping to a child table twice - @OneToMany & @ManyToOne

I'm trying to model a parent entity which has a collection of child entities (childHistory) and also a pointer to the last added child (currentChild) 我正在尝试建模一个父实体,它有一个子实体集合(childHistory),还有一个指向最后添加的子节点的指针(currentChild)

class Parent {
    //unidirectional
    @OneToOne(cascade = CascadeType.PERSIST, optional = false)
    @JoinColumn(name = "current_child_id")
    private Child currentChild;

    //bidirectional
    @OneToMany(mappedBy = "parent")
    private List<Child> childHistory;

    public Parent() {
        currentChild = new Child(this);
        childHistory = new ArrayList<>();
        childHistory.add(currentChild);
    }

    public void add() {
        currentChild = new Child(this);
        childHistory = new ArrayList<>();
        childHistory.add(currentChild);
    }
}

class Child {
    @ManyToOne(optional = false)
    @JoinColumn(name = "parent_id")
    private Parent parent;
    public Child(Parrent parent) {
        this.parent = parent;
    }
}

I currently get an exception for transient entities when I try to save the Parent (and count on cascading to persist the children). 当我尝试保存Parent时,我目前获得了瞬态实体的例外(并且依靠级联来保留子节点)。 I can't save the Parent beforehand since I init everything in the Parent ctor. 我无法预先保存父亲,因为我在父母的ctor中初始化了所有内容。

Warnings (that lead to exceptions...): 警告(导致异常......):

WARN: HHH000437: Attempting to save one or more entities that have a non-nullable association with an unsaved transient entity. 警告:HHH000437:尝试保存一个或多个与未保存的临时实体具有非可空关联的实体。 The unsaved transient entity must be saved in an operation prior to saving these dependent entities. 在保存这些依赖实体之前,必须将未保存的瞬态实体保存在操作中。 Unsaved transient entity: ([com.Parent#<null>]) Dependent entities: ([[com.Child#<null>]]) Non-nullable association(s): ([com.Child.entity]) 未保存的瞬态实体: ([com.Parent#<null>])从属实体: ([[com.Child#<null>]])非可空关联: ([com.Child.entity])


WARN: HHH000437: Attempting to save one or more entities that have a non-nullable association with an unsaved transient entity. 警告:HHH000437:尝试保存一个或多个与未保存的临时实体具有非可空关联的实体。 The unsaved transient entity must be saved in an operation prior to saving these dependent entities. 在保存这些依赖实体之前,必须将未保存的瞬态实体保存在操作中。 Unsaved transient entity: ([com.Child#<null>]) Dependent entities: ([[com.Parent#<null>]]) Non-nullable association(s): ([com.Parent.currentChild]) 未保存的瞬态实体: ([com.Child#<null>])从属实体: ([[com.Parent#<null>]])非可空关联: ([com.Parent.currentChild])

Is there a way to model this correctly and have NOT NULL db column with hibernate. 有没有一种方法可以正确建模并使用带有hibernate的NOT NULL db列。

EDIT: For a repro see this gist: https://gist.github.com/jlogar/2da2237640aa013f2cfbda33a4a5dc84 编辑:对于一个repro看到这个要点: https//gist.github.com/jlogar/2da2237640aa013f2cfbda33a4a5dc84

The exception is refering to saving transient entity, this means you are trying to save entity which has relation with a non managed entity, since your parent is cascading the onetoone child then the problem would be with childHistory, since this is a bidirectional relation it would make since to cascade the children also 例外是指保存瞬态实体,这意味着您正在尝试保存与非托管实体有关系的实体,因为您的父级正在级联onetoone子级,那么问题将出现在childHistory上,因为这是一个双向关系,它会从那以后也让孩子们级联起来

@OneToMany(mappedBy = "parent",cascade=CascadeType.PERSIST)
private List<Child> childHistory;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM