I have a Department entity which relations are as follows:
Many departments can be in one parent department:
With JPA, in parent Entity
you might have something like
@OneToMany(mappedBy="parent", cascade={CascadeType.PERSIST})
Collection 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));
}