On delete set null in hibernate in @OneToMany

Just code it:

for (Department child : parent.getChildren()) {
    child.setParentDepartment(null);
}
session.delete(parent);

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.


With JPA, in parent Entity you might have something like

@OneToMany(mappedBy="parent", cascade={CascadeType.PERSIST})
Collection<Child> children;

and in order to avoid possible repeating "set null code" & integrity violation exceptions on parent removal implement in parent Entity also

@PreRemove
private void preRemove() {
   children.forEach( child -> child.setParent(null));
}