Hi.
I've got a problem with the hibernate Event system. The following constellation mapping:
A->B->C
All mappings are implemented as sets with all-delete-orphan cascades.
Deleting A deletes all objects related to A (B+C).
Now I have an additional mapping D this references the object C with a many-to-one mapping no cascades. No mapping is done from C to D. So C knows nothing about D. If I create a record for D, referencing C, I can't delete A, B or C anymore because of the foreign key constraint. Hibernate won't remove D automatically - I have to live with that and I would have to delete D first manually before deleting A.
Now the Events come to rescue. In this I check in onDelete if it's an instance of C and execute a delete on table D with the id of C. Here comes the problem. If I delete A i have no problem - everything works and my onDelete is called. But If I delete C directly, I get an StackOverflowException, because when I delete records in D, the onDelete is called AGAIN for object C in an endless loop. I don't know why this happens. Here's the onDelete implementation:
Code:
public class VoDeleteListener extends DefaultDeleteEventListener implements DeleteEventListener {
public void onDelete(DeleteEvent pEvent) throws HibernateException {
clearActions(pEvent);
super.onDelete(pEvent);
}
public void onDelete(DeleteEvent pEvent, Set pArg1)
throws HibernateException {
clearActions(pEvent);
super.onDelete(pEvent, pArg1);
}
private void clearActions(DeleteEvent pEvent) {
if (pEvent != null) {
if (pEvent.getObject() instanceof Foo) {
Foo lFoo = (Foo) pEvent.getObject();
String lHQL = "delete from sometable where foo = :foo";
Query lQuery = pEvent.getSession().createQuery(lHQL);
lQuery.setParameter("foo",lFoo);
lQuery.executeUpdate();
}
}
}
}
Why does a deletion of D (lQuery.executeUpdate();) trigger the onDelete of C again, and again, and again?