在@OneToMany的hibernate中删除set null [英] On delete set null in hibernate in @OneToMany
问题描述
我有一个部门实体,其关系如下:
-
许多部门可以在一个家长部门中:
@ManyToOne
@JoinColumn(name =ik_parent_department_id )
私人部门parentDepartment;
-
一个父部门 / strong>:
@OneToMany(mappedBy =parentDepartment)
private Set< Department> children = new HashSet< Department>(0);
我想实现下一个:When我删除部门,然后部门的所有子女的 ik_parent_department_id 参数已设置为 null 。任何想法如何做到这一点?
您必须设置孩子的 ik_parent_department_id
显式为null。
部门parentDepartment =(部门)session.load(Department.class,id);
session.delete(parentDepartment);
for(Department child:parentDepartment.getChildren()){
child.setParentDepartment(null);
}
session.flush();
通过级联,您只能设法删除子部门
。
I have a Department entity which relations are as follows:
Many departments can be in one parent department:
@ManyToOne @JoinColumn(name = "ik_parent_department_id") private Department parentDepartment;
One parent department can have many departments:
@OneToMany(mappedBy = "parentDepartment") private Set<Department> children = new HashSet<Department>(0);
And I want to implement the next: When I delete a department, then the ik_parent_department_id parameter of all children of this department is set to null. Any ideas how to do that?
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
.
这篇关于在@OneToMany的hibernate中删除set null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!