Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
627 views
in Technique[技术] by (71.8m points)

jpa - Hibernate EntityManager: remove referenced entity not working

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I figured out the problem(s) - Actual there have been two of them: (Leaving this here, if somebody stumbles across a similar issue)

As I mentioned, the deletion should have happened in an ajax request. Therefore, simple calling

em.remove(instanceOfE6);

resulted in an java.lang.IllegalArgumentException: Removing a detached instance exception. So, I tried the approach using merge:

em.remove(em.merge(instanceOfE6));

which did not produce an error - but simple didn't work. I enabled trace-logging for hibernate as mentioned in this post: JPA/Hibernate remove entity sometimes not working to figure out what went wrong, and indeed, trying to remove the entity resulted in:

 TRACE [org...DefaultPersistEventListener] un-scheduling entity deletion ...

Obviously the entity to remove is still connected to other entities, which lead to the problem that hibernate aborted the deletion, so i tried to remove the relations before deletion. As you can see from the example of E6, I used @ManyToOne(optional = false), which means I cannot set the outgoing references to null, or it will cause exceptions when calling merge.

My first attempt to clean the relations was only "cleaning the sets", because I assumed that cleaning the outgoing relations of the entity I want to delete is not required. It is not, but i'll outline this in detail a little later:

So, the code now looked like

instanceOfE6.getE1().getE6s().remove(instanceOfE6);
instanceOfE6.getE2().getE6s().remove(instanceOfE6);
instanceOfE6.getE3().getE6s().remove(instanceOfE6);
instanceOfE6.getE4().getE6s().remove(instanceOfE6);
instanceOfE6.getE5().getE6s().remove(instanceOfE6);

em.remove(em.merge(instanceOfE6));

Same issue: deletion aborted. I missed the obvious fact, that calling merge will ofc. restore the entries inside the sets i just removed, because instanceOfE6 STILL had it's not nullable-references on the other entities. Therefore deletion was aborted again.

Finally the solution ofc. was to do the the merge before removing the references and the final deletion:

if (!em.contains(instanceOfE6)){
   instanceOfE6 = em.merge(instanceOfE6);
}

instanceOfE6.getE1().getE6s().remove(instanceOfE6);
instanceOfE6.getE2().getE6s().remove(instanceOfE6);
instanceOfE6.getE3().getE6s().remove(instanceOfE6);
instanceOfE6.getE4().getE6s().remove(instanceOfE6);
instanceOfE6.getE5().getE6s().remove(instanceOfE6);

em.remove(instanceOfE6);

I'm not quite sure if I explained everything 100% accurate, but at least I can reproduce and fix the Issue by changing the code back and forth.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...