Java – JPA – delete children from the onetomany relationship
In the @ onetomany relationship, if I want to delete a child, do I need to explicitly delete the child from the parents' collection, or is it sufficient to delete the child only?
For example, people and telephones Everyone has a lot of phone numbers If I want to delete a phone number from a person, it's enough:
EntityManager.remove(phone);
Or I need to do this in advance:
Person.getPhone().remove(phone);
Not to mention, the cascadetype is set to merge
Solution
You need to explicitly delete a phone from the phone collection. Using entitymanager to delete a phone is not enough
On the other hand, using orphan removal may be sufficient, so if you delete an entity from the collection, it will be deleted automatically It's like:
@OneToMany(mappedBy="person",orphanRemoval="true") private List<Phone> phones;
See also: http://docs.oracle.com/cd/E19798-01/821-1841/giqxy/index.html
If the parent entity is also deleted, cascade Remove deletes only child entities Cascase. Merge has nothing to do with this problem