Hibernate 从多个 Many-to-Many 关系的相关联接 table 中删除数据

Hibernate deleting data from related join table for multiple Many-to-Many relationships

我有一个 child 实体和 2 个 parent 实体,每个 parent 实体都有一个 child 实体列表。如果我通过首先获取 Parent_1 的 children 然后将该列表分配给 Parent_2 hibernate 来为 Parent_2 创建 child 实体列表,则会删除 "Parent_1-Child" 按照以下代码加入 table 条记录。

Parent_1:

@Entity
@Table
public class Parent_1 extends BusinessObject {

    List<Child> children = new ArrayList<Child>();

    public Parent_1() {}

    public Parent_1(List<Child> children) {
        this.children = children;
    }

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "parent1_children", joinColumns = @JoinColumn(name = "parent1_id"), inverseJoinColumns = @JoinColumn(name = "child_id"))
    @OrderColumn(name = "child_order")
    public List<Child> getChildren() {
        return children;
    }

    public void setChildren(List<Child> children) {
        this.children = children;
    }

}

Parent_2:

@Entity
@Table
public class Parent_2 extends BusinessObject {

    List<Child> children = new ArrayList<Child>();

    public Parent_2() {}

    public Parent_2(List<Child> children) {
        this.children = children;
    }


    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "parent2_children", joinColumns = @JoinColumn(name = "parent2_id"), inverseJoinColumns = @JoinColumn(name = "child_id"))
    @OrderColumn(name = "child_order")
    public List<Child> getChildren() {
        return children;
    }

    public void setChildren(List<Child> children) {
        this.children = children;
    }

}

Child:

@Entity
@Table
public class Child extends BusinessObject {

    public Child() {}

}

而我的代码运行遇到错误:

@Test
public void demoTest() {
    Child child_1 = new Child();
    Child child_2 = new Child();
    save(child_1, child_2);

    List<Child> children = new ArrayList<Child>();
    children.add(child_1);
    children.add(child_2);

    Parent_1 parent_1 = new Parent_1(children);
    Parent_1 parent_1_1 = new Parent_1(children);
    save(parent_1, parent_1_1);

    Parent_1 existingParent = dataAccessService.getParentById(new Long(1));
    List<Child> newChildren = existingParent.getChildren();

    Parent_2 parent_2 = new Parent_2(newChildren);
    save(parent_2);
}

这是为最后一次保存而执行的SQL:

Hibernate: 
    insert 
    into
        Parent_2
        (created, deleted, hidden, lastModified, id) 
    values
        (?, ?, ?, ?, ?)
Hibernate: 
    delete 
    from
        parent1_children 
    where
        parent1_id=?
Hibernate: 
    insert 
    into
        parent2_children
        (parent2_id, child_order, child_id) 
    values
        (?, ?, ?)
Hibernate: 
    insert 
    into
        parent2_children
        (parent2_id, child_order, child_id) 
    values
        (?, ?, ?)

问题出在删除语句上。为什么 hibernate 会执行此操作?我应该怎么做才能阻止它发生?

Rgds, 菲尔

注意:BusinessObject 只是根据以下内容证明每个 table 的 ID:

@Id
@GeneratedValue(strategy = GenerationType.TABLE)
public Long getId(){
    return id;
}

我能给你的最佳答案是它似乎在休眠上作为 JPA 工作正常。我重新创建了您的 类。我将注释放在字段上而不是方法上,但我认为这没有任何区别。我 运行 它作为 Wildfly 9.0 上的 JPA。2.Final。我看到您正在使用 Spring 持久性 API。我没有尝试重现该问题,但我认为您的问题是可重现的。