Package org.hibernate.action.internal

Source Code of org.hibernate.action.internal.EntityDeleteAction

/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* Copyright (c) 2008-2011, Red Hat Inc. or third-party contributors as
* indicated by the @author tags or express copyright attribution
* statements applied by the authors.  All third-party contributions are
* distributed under license by Red Hat Inc.
*
* This copyrighted material is made available to anyone wishing to use, modify,
* copy, or redistribute it subject to the terms and conditions of the GNU
* Lesser General Public License, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this distribution; if not, write to:
* Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor
* Boston, MA  02110-1301  USA
*/
package org.hibernate.action.internal;

import java.io.Serializable;

import org.hibernate.AssertionFailure;
import org.hibernate.HibernateException;
import org.hibernate.cache.CacheKey;
import org.hibernate.cache.access.SoftLock;
import org.hibernate.engine.EntityEntry;
import org.hibernate.engine.PersistenceContext;
import org.hibernate.engine.SessionImplementor;
import org.hibernate.event.EventSource;
import org.hibernate.event.EventType;
import org.hibernate.event.PostDeleteEvent;
import org.hibernate.event.PostDeleteEventListener;
import org.hibernate.event.PreDeleteEvent;
import org.hibernate.event.PreDeleteEventListener;
import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.service.event.spi.EventListenerGroup;

public final class EntityDeleteAction extends EntityAction {
  private final Object version;
  private final boolean isCascadeDeleteEnabled;
  private final Object[] state;

  private SoftLock lock;

  public EntityDeleteAction(
      final Serializable id,
          final Object[] state,
          final Object version,
          final Object instance,
          final EntityPersister persister,
          final boolean isCascadeDeleteEnabled,
          final SessionImplementor session) {
    super( session, id, instance, persister );
    this.version = version;
    this.isCascadeDeleteEnabled = isCascadeDeleteEnabled;
    this.state = state;
  }

  @Override
  public void execute() throws HibernateException {
    Serializable id = getId();
    EntityPersister persister = getPersister();
    SessionImplementor session = getSession();
    Object instance = getInstance();

    boolean veto = preDelete();

    Object version = this.version;
    if ( persister.isVersionPropertyGenerated() ) {
      // we need to grab the version value from the entity, otherwise
      // we have issues with generated-version entities that may have
      // multiple actions queued during the same flush
      version = persister.getVersion( instance, session.getEntityMode() );
    }

    final CacheKey ck;
    if ( persister.hasCache() ) {
      ck = session.generateCacheKey( id, persister.getIdentifierType(), persister.getRootEntityName() );
      lock = persister.getCacheAccessStrategy().lockItem( ck, version );
    }
    else {
      ck = null;
    }

    if ( !isCascadeDeleteEnabled && !veto ) {
      persister.delete( id, version, instance, session );
    }
   
    //postDelete:
    // After actually deleting a row, record the fact that the instance no longer
    // exists on the database (needed for identity-column key generation), and
    // remove it from the session cache
    final PersistenceContext persistenceContext = session.getPersistenceContext();
    EntityEntry entry = persistenceContext.removeEntry( instance );
    if ( entry == null ) {
      throw new AssertionFailure( "possible nonthreadsafe access to session" );
    }
    entry.postDelete();

    persistenceContext.removeEntity( entry.getEntityKey() );
    persistenceContext.removeProxy( entry.getEntityKey() );
   
    if ( persister.hasCache() ) {
      persister.getCacheAccessStrategy().remove( ck );
    }

    postDelete();

    if ( getSession().getFactory().getStatistics().isStatisticsEnabled() && !veto ) {
      getSession().getFactory().getStatisticsImplementor().deleteEntity( getPersister().getEntityName() );
    }
  }

  private boolean preDelete() {
    boolean veto = false;
    EventListenerGroup<PreDeleteEventListener> listenerGroup = listenerGroup( EventType.PRE_DELETE );
    if ( listenerGroup.isEmpty() ) {
      return veto;
    }
    final PreDeleteEvent event = new PreDeleteEvent( getInstance(), getId(), state, getPersister(), eventSource() );
    for ( PreDeleteEventListener listener : listenerGroup.listeners() ) {
      veto |= listener.onPreDelete( event );
    }
    return veto;
  }

  private void postDelete() {
    EventListenerGroup<PostDeleteEventListener> listenerGroup = listenerGroup( EventType.POST_DELETE );
    if ( listenerGroup.isEmpty() ) {
      return;
    }
    final PostDeleteEvent event = new PostDeleteEvent(
        getInstance(),
        getId(),
        state,
        getPersister(),
        eventSource()
    );
    for ( PostDeleteEventListener listener : listenerGroup.listeners() ) {
      listener.onPostDelete( event );
    }
  }

  private void postCommitDelete() {
    EventListenerGroup<PostDeleteEventListener> listenerGroup = listenerGroup( EventType.POST_COMMIT_DELETE );
    if ( listenerGroup.isEmpty() ) {
      return;
    }
    final PostDeleteEvent event = new PostDeleteEvent(
        getInstance(),
        getId(),
        state,
        getPersister(),
        eventSource()
    );
  }

  @Override
  public void doAfterTransactionCompletion(boolean success, SessionImplementor session) throws HibernateException {
    if ( getPersister().hasCache() ) {
      final CacheKey ck = getSession().generateCacheKey(
          getId(),
          getPersister().getIdentifierType(),
          getPersister().getRootEntityName()
      );
      getPersister().getCacheAccessStrategy().unlockItem( ck, lock );
    }
    postCommitDelete();
  }

  @Override
  protected boolean hasPostCommitEventListeners() {
    return ! listenerGroup( EventType.POST_COMMIT_DELETE ).isEmpty();
  }
}
TOP

Related Classes of org.hibernate.action.internal.EntityDeleteAction

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.