On delete set null in hibernate in @OneToMany

后端 未结 3 2016
南笙
南笙 2021-01-07 17:43

I have a Department entity which relations are as follows:

  1. Many departments can be in one parent department:

    
    
            
3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-07 18:24

    You'll have to set the children's ik_parent_department_id to null explicitly.

    Department parentDepartment = (Department) session.load(Department.class, id);
    session.delete(parentDepartment);
    for (Department child : parentDepartment.getChildren()){
        child.setParentDepartment(null);
    } 
    session.flush();
    

    With cascading you would only manage to delete child Departments.

提交回复
热议问题