Orphanremoval dont work with merge priori

Hi,

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 am going to run an integration test to reproduce the issue and see if it should work.

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.

1 Like

It’s very clear thank you.

We could perhaps add an exception if we have this configuration (mapped by and orphanRemoval=true) to remove any ambiguity ^^

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.

yes very well.
I will do it soon.
If it doesn’t work in 5.0 then in 6.0 it’s normal that it doesn’t work either.

yet we have a similar case described in the documentation for info :

Hibernate ORM 6.2.4.Final User Guide (jboss.org)