I have a model "Form" with a list of childs "Group", which has a list of childs "Element", at form it is mapped as:
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "form", orphanRemoval = true)
private Set<GroupingModel> grouping;
at the child group I have this:
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
@JoinColumn(name = "NR_SEQU_FORL")
private FormModel form;
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "NR_SEQU_AGRU")
private Set<FormElementModel> customElements;
and at the child element I have this:
@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.REFRESH)
@JoinColumn(name = "NR_SEQU_AGRU")
private GroupingModel group;
I need the orphan removal option since I will have no way to know what childs are being removed from a group, but I also need to allow child elements to be "adopted", that is, I need to be able to call formRepository.save with a new Form, moving elements from group A to group B.
What is happening right now is JPA tries to remove the element from group A instead of updating it to groub B, triggering the orphan removal and raising an SQL exception ("ERROR: update or delete on table "tb_element" violates foreign key constraint "fk_elem_04" on table "tb_element" Detalhe: Key (id_elem)=(1) is still referenced from table "tb_element".")
I have seen a few people talking about this JPA bug, mainly talking about it happening with hibernate or eclipse link, but I still haven't found a way to fix it without having the make huge changes to the code I have right now.
So that's the thing, how can I disable orphan removal when said orphan is adopted?