Hey again,
What do you guys think of following work-around? Is it safe enough to implement?
I noticed the extra select-query is triggered in the DefaultDeleteEventListener.onDelete(DeleteEvent event, Set transientEntities):
Code:
if ( entityEntry == null ) {
log.trace( "entity was not persistent in delete processing" );
persister = source.getEntityPersister( event.getEntityName(), entity );
if ( ForeignKeys.isTransient( persister.getEntityName(), entity, null, source ) ) {
deleteTransientEntity( source, entity, event.isCascadeDeleteEnabled(), persister, transientEntities );
// EARLY EXIT!!!
return;
}
else {
...
}
So I made a CustomDeleteEventListener which extends the DefaultDeleteEventListener & overrides following method:
Code:
/* (non-Javadoc)
* @see org.hibernate.event.def.DefaultDeleteEventListener#deleteTransientEntity(org.hibernate.event.EventSource, java.lang.Object, boolean, org.hibernate.persister.entity.EntityPersister, java.util.Set)
*/
@Override
protected void deleteTransientEntity(EventSource session, Object entity,
boolean cascadeDeleteEnabled, EntityPersister persister,
Set transientEntities) {
/* perform the normal deleteTransientEntity-method */
super.deleteTransientEntity(session, entity, cascadeDeleteEnabled, persister,
transientEntities);
/*
* Backwards compability: in previous Hibernate-versions, trying to
* delete a transient entity also triggered a StaleStateException.
* In newer versions, this isn't the case anymore. As a work-around,
* we'll throw the StaleStateException ourselves.
*/
if (transientEntities != null && transientEntities.size() > 0) {
throw new StaleStateException(
"Unexpected row count: 0 expected: 1"
);
}
}
Thanks,
Sven