Note that there are some explanatory texts on larger screens.

plurals
  1. PODelete Entities in EventListener
    text
    copied!<p>I'm working with Hibernate 4.1.1 for quite a while now. Following: I've created an EventListener which is integrated by the Integrator Interface of Hibernate. So far, this works!</p> <p>I'm trying to delete an Entity, for example an Account. An account has a one to many relation to playlists.</p> <p>What I want to achieve: Delete Playlists when Account is deleted.</p> <p>I know that I can achieve this by adding cascade behavior to my Hibernate Mappings. So far, this will work, but Hibernate isn't able to provide functionality to set a value null on cascade, so I wanted to do those steps manually:</p> <pre><code>@Override public boolean onPreDelete(final PreDeleteEvent preDeleteEvent) throws HibernateException { if (preDeleteEvent.getEntity() instanceof PresetEntry) { } else if (preDeleteEvent.getEntity() instanceof AccountEntry) { final Session session = this.sessionFactory.getCurrentSession(); if (!session.getTransaction().isActive()) { session.getTransaction().begin(); } final AccountEntry accountEntry = (AccountEntry) preDeleteEvent.getEntity(); session.createQuery("DELETE FROM PlaylistEntry WHERE ACCOUNT_ID = " + accountEntry.getIdentity()).executeUpdate(); //NON-NLS } else if (preDeleteEvent.getEntity() instanceof PlaylistEntry) { } else if (preDeleteEvent.getEntity() instanceof QueueEntry) { } return false; } </code></pre> <p>I'm running in following issues:</p> <ol> <li>A transaction is already open because the account is currently deleted</li> <li>Calling commit inside onPreDelete results in StackOverflowError (it's endlessly committing the delete playlistEntry query)</li> <li>If the query isn't commited inside the onPreDelete method the queries will be executed, however the Swing Application begins to freeze forever.</li> <li>Not removing the playlists results in foreign key no action exception</li> </ol> <p>So how can I solve that?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload