I have the impression that orphan removal doesn’t work with merge.
Here’s my situation:
I have a detached object called “ParametrageJoursOuvres” which has a list of 7 instances of “JourNonOuvre”.
I remove 3 instances from the list (also removing the backRefTo), and then I perform a Session#Merge by passing the “ParametrageJoursOuvres” instance.
However, it doesn’t delete the 3 orphaned instances, and when I reload, they reappear in the list.
Is there a concept that I haven’t understood correctly?
@Entity
public class ParametrageJoursOuvres {
@OneToMany(mappedBy = "parametrage", fetch = EAGER, cascade = ALL, orphanRemoval = true)
private Set<JourNonOuvre> joursNonOuvres = new HashSet<>();
@Entity
public class JourNonOuvre {
@ManyToOne(fetch = LAZY)
private ParametrageJoursOuvres parametrage;
}
I don’t think orphan removal has ever worked in conjunction with mappedBy, as the two things are not compatible AFAIU.
Hibernate can’t determine if something is an orphan on the collection side, because the collection side is not the owner of the association. Orphan removal only works if the collection is the owning side.
The JPA spec if not totally clear on this so I can’t say for sure. It would be best if you try to model your case with Hibernate 5 first and if it works, create a testcase for Hibernate 6 and report back.