Source code: org/hibernate/action/EntityDeleteAction.java
1 //$Id: EntityDeleteAction.java,v 1.19 2005/02/21 13:15:22 oneovthafew Exp $
2 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.engine.Status;
15 import org.hibernate.event.PostDeleteEvent;
16 import org.hibernate.event.PreDeleteEvent;
17 import org.hibernate.persister.entity.EntityPersister;
18
19 public final class EntityDeleteAction extends EntityAction {
20
21 private final Object version;
22 private SoftLock lock;
23 private final boolean isCascadeDeleteEnabled;
24 private final Object[] state;
25
26 public EntityDeleteAction(final Serializable id,
27 final Object[] state,
28 final Object version,
29 final Object instance,
30 final EntityPersister persister,
31 final boolean isCascadeDeleteEnabled,
32 final SessionImplementor session) {
33
34 super( session, id, instance, persister );
35 this.version = version;
36 this.isCascadeDeleteEnabled = isCascadeDeleteEnabled;
37 this.state = state;
38 }
39
40 public void execute() throws HibernateException {
41 Serializable id = getId();
42 EntityPersister persister = getPersister();
43 SessionImplementor session = getSession();
44 Object instance = getInstance();
45
46 PreDeleteEvent preEvent = new PreDeleteEvent(instance, id, state, persister, session);
47 final boolean veto = session.getListeners().getPreDeleteEventListener().onPreDelete(preEvent);
48
49 final CacheKey ck;
50 if ( persister.hasCache() ) {
51 ck = new CacheKey( id, getPersister().getIdentifierType(), persister.getRootEntityName(), session.getEntityMode() );
52 lock = persister.getCache().lock(ck, version);
53 }
54 else {
55 ck = null;
56 }
57
58 if ( !isCascadeDeleteEnabled && !veto ) {
59 persister.delete( id, version, instance, session );
60 }
61
62 //postDelete:
63 // After actually deleting a row, record the fact that the instance no longer
64 // exists on the database (needed for identity-column key generation), and
65 // remove it from the session cache
66 final PersistenceContext persistenceContext = session.getPersistenceContext();
67 EntityEntry entry = persistenceContext.removeEntry( instance );
68 if ( entry == null ) throw new AssertionFailure( "possible nonthreadsafe access to session" );
69 persistenceContext.setEntryStatus( entry, Status.GONE );
70 entry.setExistsInDatabase( false );
71
72 EntityKey key = new EntityKey( entry.getId(), entry.getPersister(), session.getEntityMode() );
73 persistenceContext.removeEntity(key);
74 persistenceContext.removeProxy(key);
75
76 if ( persister.hasCache() ) persister.getCache().evict(ck);
77
78 PostDeleteEvent postEvent = new PostDeleteEvent( instance, id, state, getPersister(), session );
79 session.getListeners().getPostDeleteEventListener().onPostDelete( postEvent );
80
81 if ( getSession().getFactory().getStatistics().isStatisticsEnabled() && !veto ) {
82 getSession().getFactory().getStatisticsImplementor()
83 .deleteEntity( getPersister().getEntityName() );
84 }
85 }
86
87 public void afterTransactionCompletion(boolean success) throws HibernateException {
88 final CacheKey ck = new CacheKey(
89 getId(),
90 getPersister().getIdentifierType(),
91 getPersister().getRootEntityName(),
92 getSession().getEntityMode()
93 );
94 if ( getPersister().hasCache() ) getPersister().getCache().release(ck, lock);
95 }
96
97 }
98
99
100
101
102
103
104