注释
Parent.children是问题的根源。添加
mappedBy,
@JoinColumns在父侧删除。
正确的设置方式:
@oneToMany(mappedBy = "parent", fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)List<Child> children = new ArrayList<>();
我相信为删除而生成的查询是理想的结果。
Hibernate: delete from z_child where pre=? and parent_pre=? and parent_type=?
此外,
removeChild可以简化-无需将child的父级设置为null-无论如何都会处理。这不会影响生成的查询。
public void removeChild(Child child){ // child.setParent(null); No need to do that children.remove(child);}


