1 package org.hibernate.action; 3 4 import java.io.Serializable ; 5 6 import org.hibernate.AssertionFailure; 7 import org.hibernate.HibernateException; 8 import org.hibernate.cache.CacheKey; 9 import org.hibernate.cache.CacheConcurrencyStrategy.SoftLock; 10 import org.hibernate.engine.EntityEntry; 11 import org.hibernate.engine.EntityKey; 12 import org.hibernate.engine.PersistenceContext; 13 import org.hibernate.engine.SessionImplementor; 14 import org.hibernate.event.PostDeleteEvent; 15 import org.hibernate.event.PreDeleteEvent; 16 import org.hibernate.persister.entity.EntityPersister; 17 18 public final class EntityDeleteAction extends EntityAction { 19 20 private final Object version; 21 private SoftLock lock; 22 private final boolean isCascadeDeleteEnabled; 23 private final Object [] state; 24 25 public EntityDeleteAction(final Serializable id, 26 final Object [] state, 27 final Object version, 28 final Object instance, 29 final EntityPersister persister, 30 final boolean isCascadeDeleteEnabled, 31 final SessionImplementor session) { 32 33 super( session, id, instance, persister ); 34 this.version = version; 35 this.isCascadeDeleteEnabled = isCascadeDeleteEnabled; 36 this.state = state; 37 } 38 39 public void execute() throws HibernateException { 40 Serializable id = getId(); 41 EntityPersister persister = getPersister(); 42 SessionImplementor session = getSession(); 43 Object instance = getInstance(); 44 45 PreDeleteEvent preEvent = new PreDeleteEvent(instance, id, state, persister); 46 final boolean veto = session.getListeners() 47 .getPreDeleteEventListener() 48 .onPreDelete(preEvent); 49 50 final CacheKey ck; 51 if ( persister.hasCache() ) { 52 ck = new CacheKey( 53 id, 54 getPersister().getIdentifierType(), 55 persister.getRootEntityName(), 56 session.getEntityMode(), 57 session.getFactory() 58 ); 59 lock = persister.getCache().lock(ck, version); 60 } 61 else { 62 ck = null; 63 } 64 65 if ( !isCascadeDeleteEnabled && !veto ) { 66 persister.delete( id, version, instance, session ); 67 } 68 69 final PersistenceContext persistenceContext = session.getPersistenceContext(); 74 EntityEntry entry = persistenceContext.removeEntry( instance ); 75 if ( entry == null ) { 76 throw new AssertionFailure( "possible nonthreadsafe access to session" ); 77 } 78 entry.postDelete(); 79 80 EntityKey key = new EntityKey( entry.getId(), entry.getPersister(), session.getEntityMode() ); 81 persistenceContext.removeEntity(key); 82 persistenceContext.removeProxy(key); 83 84 if ( persister.hasCache() ) persister.getCache().evict(ck); 85 86 PostDeleteEvent postEvent = new PostDeleteEvent( instance, id, state, getPersister() ); 87 session.getListeners() 88 .getPostDeleteEventListener() 89 .onPostDelete( postEvent ); 90 91 if ( getSession().getFactory().getStatistics().isStatisticsEnabled() && !veto ) { 92 getSession().getFactory().getStatisticsImplementor() 93 .deleteEntity( getPersister().getEntityName() ); 94 } 95 } 96 97 public void afterTransactionCompletion(boolean success) throws HibernateException { 98 if ( getPersister().hasCache() ) { 99 final CacheKey ck = new CacheKey( 100 getId(), 101 getPersister().getIdentifierType(), 102 getPersister().getRootEntityName(), 103 getSession().getEntityMode(), 104 getSession().getFactory() 105 ); 106 getPersister().getCache().release(ck, lock); 107 } 108 } 109 110 } 111 112 113 114 115 116 117 118 | Popular Tags |