The child is an entity of its own and can exist even without a parent. So removing the relationship does not mean removing the child. To no longer appear in the database the child must be removed from the parent’s list and must also be removed from the persistence context directly by entityManager.
For example, to remove child 0 from parent and database:
Pai pai = entityManager.find(Pai.class, 19L);
Filho filho = pai.getFilhos().remove(0); // remove e retorna o elemento removido
entityManager.remove(filho);
But why in his example the son is not removed at least from the father’s list?
Well, in the JPA specification:
The Many side of one-to-Many / Many-to-one bidirectional relationships
must be the Owning side, Hence the mappedBy element cannot be
specified on the Manytoone Annotation.
(JSR 338 - Section 2.9 Entity Relantionships)
In your case who is on the "Many" side of the relationship is the Son. Therefore he is the owner of the relationship! Removing a child from the Parent class will make no difference in the database because the parent is not the owner of the relationship.
To undo the relationship the father must be removed from the son. The interesting thing would always be to do both, to remove the father from the son and the son from the father, so the entity graph is always consistent.
To remove by default the way you want, Hibernate has the orphanRemoval option, where any fatherless child is automatically deleted, so simply remove the child from the parent list. For that just change @Onetomany in the parent class as follows:
@OneToMany(mappedBy = "pai", cascade = CascadeType.ALL, orphanRemoval = true)
public List<Staff> getFilhos() {
return staffs;
}
This solution is specific to Hibernate and does not exist in JPA, but would be the most appropriate solution to your question.
Another option is to declare the relationship only on the father’s side, erasing the manyToOne on the son. In this case Hibernate will create an extra pai_filho table which can be unwanted, when deleting the child from the father’s list he will be removed from the relationship, but the child will continue to exist in his table, but without father.