Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I delete orphan entities using hibernate and JPA on a many-to-many relationship?

I want to delete orphan entities using hibernate and JPA on a many-to-many relationship but all that I found was this attribute the attribute. org.hibernate.annotations.CascadeType.DELETE_ORPHAN ( i.e. @Cascade(value={org.hibernate.annotations.CascadeType.DELETE_ORPHAN) ), which works only for one-to-many relationships.

I want to know if I can delete the orphan ones on my many-to-many relationship.

like image 774
Alucard Avatar asked Jun 16 '10 16:06

Alucard


People also ask

What is orphan removal in JPA?

Orphan removal means that dependent entities are removed when the relationship to their "parent" entity is destroyed. For example if a child is removed from a @OneToMany relationship without explicitely removing it in the entity manager.

What is delete orphan in Hibernate?

If an entity is removed from a @OneToMany collection or an associated entity is dereferenced from a @OneToOne association, this associated entity can be marked for deletion if orphanRemoval is set to true.


1 Answers

From the book "Pro JPA 2":

Only relationships with single cardinality on the source side can enable orphan removal, which is why the orphanRemoval option is defined on the @OneToOne and @OneToMany relationship annotations, but on neither of the @ManyToOne or @ManyToMany annotations.

It's a bummer, but there is no JPA automated orphan removal for ManyToMany.

like image 104
HDave Avatar answered Oct 11 '22 15:10

HDave