KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > action > EntityDeleteAction


1 //$Id: EntityDeleteAction.java,v 1.22 2005/06/15 13:20:12 oneovthafew Exp $
2
package org.hibernate.action;
3
4 import java.io.Serializable JavaDoc;
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 JavaDoc version;
21     private SoftLock lock;
22     private final boolean isCascadeDeleteEnabled;
23     private final Object JavaDoc[] state;
24
25     public EntityDeleteAction(final Serializable JavaDoc id,
26                               final Object JavaDoc[] state,
27                               final Object JavaDoc version,
28                               final Object JavaDoc 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 JavaDoc id = getId();
41         EntityPersister persister = getPersister();
42         SessionImplementor session = getSession();
43         Object JavaDoc 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         //postDelete:
70
// After actually deleting a row, record the fact that the instance no longer
71
// exists on the database (needed for identity-column key generation), and
72
// remove it from the session cache
73
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