I'm currenetly trying hard to delete an entity, that is involved in various relations (Only @ManyToOne
) - However, Hibernate does not delete anything - after calling em.remove
everything remains unchanged.
I have 5 entities (E1 - E5
), referencing the entity (E6
) in question like this:
@Entity
public class EX {
@OneToMany(mappedBy = "eX", fetch = FetchType.EAGER, cascade = { CascadeType.PERSIST, CascadeType.REMOVE })
private Set<E6> e6s;
}
E6
itself has the reverse @ManyToOne
Relations:
public class E6{
@ManyToOne(optional = false)
private E1 e1;
@ManyToOne(optional = false)
private E2 e2;
@ManyToOne(optional = false)
private E3 e3;
@ManyToOne(optional = false)
private E4 e4;
@ManyToOne(optional = false)
private E5 e5;
}
(left out ids, additional columns etc...)
Whenever I call em.remove(instanceOfE6)
, simply nothing happens. I added Hibernate SQL-Output and cannot see a single attempt of executing a DELETE
Query.
Since the deletion can happen from an ajax request and a fresh EntityManager, I added a call to merge before the deletion, cause otherwhise I get an Entity unmanaged Exception:
em.remove(em.merge(instanceOfE6));
But nothing. State remains unchanged.
So I tried to add an @PreRemove
Method to clear all the relations before removing the entity... Method gets never called.
I tried various Cascade Operations (Including Cascade.ALL
) - but since all I need is remove and persist, that should be working as well.
I thought about using a native Delete Statement (each entity has a surrogate id, so that would most likely work) - but since everything happens from within an AJAX-call, I don't want to use that, cause I want to have the Persistence-Cache updated without re-fetching every involved Entity...
Any ideas?
If you need more source, just drop me a comment.
I also tried to remove the entity in question from ALL relations (object wise), and call em.merge()
on one of the remaining entities, hoping that hibernate will whipe the unlinked entity - no luck.
See Question&Answers more detail:
os