package org.hibernate.action.internal;
import java.io.Serializable;
import org.hibernate.AssertionFailure;
import org.hibernate.HibernateException;
import org.hibernate.collection.spi.PersistentCollection;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.event.service.spi.EventListenerGroup;
import org.hibernate.event.spi.EventType;
import org.hibernate.event.spi.PostCollectionRemoveEvent;
import org.hibernate.event.spi.PostCollectionRemoveEventListener;
import org.hibernate.event.spi.PreCollectionRemoveEvent;
import org.hibernate.event.spi.PreCollectionRemoveEventListener;
import org.hibernate.persister.collection.CollectionPersister;
public final class CollectionRemoveAction extends CollectionAction {
private final Object affectedOwner;
private boolean emptySnapshot;
public CollectionRemoveAction(
final PersistentCollection collection,
final CollectionPersister persister,
final Serializable id,
final boolean emptySnapshot,
final SharedSessionContractImplementor session) {
super( persister, collection, id, session );
if ( collection == null ) {
throw new AssertionFailure("collection == null");
}
this.emptySnapshot = emptySnapshot;
this.affectedOwner = session.getPersistenceContext().getLoadedCollectionOwnerOrNull( collection );
}
public CollectionRemoveAction(
final Object affectedOwner,
final CollectionPersister persister,
final Serializable id,
final boolean emptySnapshot,
final SharedSessionContractImplementor session) {
super( persister, null, id, session );
if ( affectedOwner == null ) {
throw new AssertionFailure("affectedOwner == null");
}
this.emptySnapshot = emptySnapshot;
this.affectedOwner = affectedOwner;
}
@Override
public void execute() throws HibernateException {
preRemove();
if ( !emptySnapshot ) {
getPersister().remove( getKey(), getSession() );
}
final PersistentCollection collection = getCollection();
if ( collection != null ) {
getSession().getPersistenceContext().getCollectionEntry( collection ).afterAction( collection );
}
evict();
postRemove();
if ( getSession().getFactory().getStatistics().isStatisticsEnabled() ) {
getSession().getFactory().getStatistics().removeCollection( getPersister().getRole() );
}
}
private void preRemove() {
final EventListenerGroup<PreCollectionRemoveEventListener> listenerGroup = listenerGroup( EventType.PRE_COLLECTION_REMOVE );
if ( listenerGroup.isEmpty() ) {
return;
}
final PreCollectionRemoveEvent event = new PreCollectionRemoveEvent(
getPersister(),
getCollection(),
eventSource(),
affectedOwner
);
for ( PreCollectionRemoveEventListener listener : listenerGroup.listeners() ) {
listener.onPreRemoveCollection( event );
}
}
private void postRemove() {
final EventListenerGroup<PostCollectionRemoveEventListener> listenerGroup = listenerGroup( EventType.POST_COLLECTION_REMOVE );
if ( listenerGroup.isEmpty() ) {
return;
}
final PostCollectionRemoveEvent event = new PostCollectionRemoveEvent(
getPersister(),
getCollection(),
eventSource(),
affectedOwner
);
for ( PostCollectionRemoveEventListener listener : listenerGroup.listeners() ) {
listener.onPostRemoveCollection( event );
}
}
}