private static void processDereferencedCollection(PersistentCollection coll, SessionImplementor session)
throws HibernateException {
final PersistenceContext persistenceContext = session.getPersistenceContext();
CollectionEntry entry = persistenceContext.getCollectionEntry(coll);
final CollectionPersister loadedPersister = entry.getLoadedPersister();
if ( log.isDebugEnabled() && loadedPersister != null )
log.debug(
"Collection dereferenced: " +
MessageHelper.collectionInfoString(
loadedPersister,
entry.getLoadedKey(),
session.getFactory()
)
);
// do a check
boolean hasOrphanDelete = loadedPersister != null &&
loadedPersister.hasOrphanDelete();
if (hasOrphanDelete) {
Serializable ownerId = loadedPersister.getOwnerEntityPersister()
.getIdentifier( coll.getOwner(), session.getEntityMode() );
if ( ownerId == null ) {
// the owning entity may have been deleted and its identifier unset due to
// identifier-rollback; in which case, try to look up its identifier from
// the persistence context
if ( session.getFactory().getSettings().isIdentifierRollbackEnabled() ) {
EntityEntry ownerEntry = persistenceContext.getEntry( coll.getOwner() );
if ( ownerEntry != null ) {
ownerId = ownerEntry.getId();
}
}
if ( ownerId == null ) {
throw new AssertionFailure( "Unable to determine collection owner identifier for orphan-delete processing" );
}
}
EntityKey key = new EntityKey(
ownerId,
loadedPersister.getOwnerEntityPersister(),
session.getEntityMode()
);
Object owner = persistenceContext.getEntity(key);
if ( owner == null ) {
throw new AssertionFailure(
"collection owner not associated with session: " +
loadedPersister.getRole()
);
}
EntityEntry e = persistenceContext.getEntry(owner);
//only collections belonging to deleted entities are allowed to be dereferenced in the case of orphan delete
if ( e != null && e.getStatus() != Status.DELETED && e.getStatus() != Status.GONE ) {
throw new HibernateException(
"A collection with cascade=\"all-delete-orphan\" was no longer referenced by the owning entity instance: " +
loadedPersister.getRole()
);
}
}
// do the work