@ManyToOne without cascade REMOVE

Hi,

To confirm the solution:

I have a scenario where I have a @OneToMany relationship (on object Partenaire) without the REMOVE cascade linked with @ManyToOne (on object ConventionPartenaire).

When I delete object Partenaire, it then tells me that it’s not possible to save object ConventionPartenaire because it holds a transient object (which is normal).

Is the solution to set the return variable to null by removing the association from ConventionPartenaire to Partenaire (@PreRemove) ?

Thank you !

@Entity
public class Partenaire {
  @OneToMany(mappedBy = "partenaire", fetch = EAGER, cascade = {REFRESH, PERSIST})
  private Set<ConventionPartenaire> conventions = new HashSet<>();

  @PreRemove
  public void preRemove() {
    conventions.forEach(this::removeFromConventions);
  }

  public boolean removeFromConventions(ConventionPartenaire conventionPartenaire) {
    conventionPartenaire.setPartenaire(null);
    return conventions.remove(conventionPartenaire);
  }
}

@Entity
public class ConventionPartenaire {
  @ManyToOne
  private Partenaire       partenaire;
}

Yeah, that looks ok to me.

1 Like