JPA - Remove Child from OneToMany Relationship

Regarding @OneToMany , if I want to remove a child, do I need to explicitly delete this child from the parent collection, or is it enough to remove the child?

For example, Personand Phone. Each person has many phone numbers. If I want to delete one phone number from a person, it’s enough:

EntityManager.remove(phone);

Or I need this in advance:

Person.getPhone().remove(phone); 

Not to mention, the CascadeType parameter is set to MERGE .

+4
source share
2 answers

Phone phones, , EntityManager.

, orphanRemoval, , , . - :

@OneToMany(mappedBy="person", orphanRemoval="true")
private List<Phone> phones;

: http://docs.oracle.com/cd/E19798-01/821-1841/giqxy/index.html

Cascade.REMOVE . Cascase.MERGE .

+7

, MERGE , , , , DELETE , ( ), .

, JPA- entityManager.remove(), , , oneToMany. : , , , , . , .

+3
source

Source: https://habr.com/ru/post/1525773/


All Articles