Search code examples
javahibernatehibernate-annotations

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


I have a child entity and 2 parent entities, each parent entity has a list of the child entities. If I create the list of child entities for Parent_2 by first getting the children of Parent_1 and then assigning that list to Parent_2 hibernate deletes the "Parent_1-Child" join table records as per the following code.

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() {}

}

And the code I run to experience the error:

@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);
}

This is the SQL being executed for the last save:

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
        (?, ?, ?)

The problem is with the delete statement. Why is hibernate executing this and what should I do to stop it from happening?

Rgds, Phil

Note: BusinessObject just proves the id for each table as per:

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

Solution

  • The best answer I can give you is that it seems to be working OK as JPA on hibernate. I recreated your classes. I put the annotations on the fields instead of the methods, but I don't think that makes any difference. I ran it as JPA on Wildfly 9.0.2.Final. I see that you're using the Spring persistence API. I didn't try to reproduce that, but I assume your problem is reproducible.