KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > event > def > DefaultEvictEventListener


1 //$Id: DefaultEvictEventListener.java,v 1.7 2005/06/05 05:09:57 oneovthafew Exp $
2
package org.hibernate.event.def;
3
4 import java.io.Serializable JavaDoc;
5
6 import org.apache.commons.logging.Log;
7 import org.apache.commons.logging.LogFactory;
8 import org.hibernate.HibernateException;
9 import org.hibernate.engine.Cascade;
10 import org.hibernate.engine.CascadingAction;
11 import org.hibernate.engine.EntityEntry;
12 import org.hibernate.engine.EntityKey;
13 import org.hibernate.engine.PersistenceContext;
14 import org.hibernate.event.EventSource;
15 import org.hibernate.event.EvictEvent;
16 import org.hibernate.event.EvictEventListener;
17 import org.hibernate.persister.entity.EntityPersister;
18 import org.hibernate.pretty.MessageHelper;
19 import org.hibernate.proxy.HibernateProxy;
20 import org.hibernate.proxy.LazyInitializer;
21
22 /**
23  * Defines the default evict event listener used by hibernate for evicting entities
24  * in response to generated flush events. In particular, this implementation will
25  * remove any hard references to the entity that are held by the infrastructure
26  * (references held by application or other persistent instances are okay)
27  *
28  * @author Steve Ebersole
29  */

30 public class DefaultEvictEventListener extends AbstractEventListener implements EvictEventListener {
31
32     private static final Log log = LogFactory.getLog(DefaultEvictEventListener.class);
33
34     /**
35      * Handle the given evict event.
36      *
37      * @param event The evict event to be handled.
38      * @throws HibernateException
39      */

40     public void onEvict(EvictEvent event) throws HibernateException {
41         EventSource source = event.getSession();
42         final Object JavaDoc object = event.getObject();
43         
44         final PersistenceContext persistenceContext = source.getPersistenceContext();
45         if ( object instanceof HibernateProxy ) {
46             
47             LazyInitializer li = ( (HibernateProxy) object ).getHibernateLazyInitializer();
48             Serializable JavaDoc id = li.getIdentifier();
49             EntityPersister persister = source.getFactory().getEntityPersister( li.getEntityName() );
50             
51             EntityKey key = new EntityKey( id, persister, source.getEntityMode() );
52             persistenceContext.removeProxy(key);
53             
54             if ( !li.isUninitialized() ) {
55                 final Object JavaDoc entity = persistenceContext.removeEntity(key);
56                 if ( entity != null ) {
57                     EntityEntry e = event.getSession().getPersistenceContext().removeEntry(entity);
58                     doEvict( entity, key, e.getPersister(), event.getSession() );
59                 }
60             }
61             
62         }
63         else {
64             
65             EntityEntry e = persistenceContext.removeEntry(object);
66             if ( e != null ) {
67                 EntityKey key = new EntityKey( e.getId(), e.getPersister(), source.getEntityMode() );
68                 persistenceContext.removeEntity(key);
69                 doEvict( object, key, e.getPersister(), source );
70             }
71             
72         }
73     }
74
75     protected void doEvict(
76         final Object JavaDoc object,
77         final EntityKey key,
78         final EntityPersister persister,
79         final EventSource session)
80     throws HibernateException {
81
82         if ( log.isTraceEnabled() ) {
83             log.trace( "evicting " + MessageHelper.infoString(persister) );
84         }
85
86         // remove all collections for the entity from the session-level cache
87
if ( persister.hasCollections() ) new EvictVisitor( session ).process( object, persister );
88         
89         // remove any snapshot, not really for memory management purposes, but
90
// rather because it might now be stale, and there is no longer any
91
// EntityEntry to take precedence
92
// This is now handled by removeEntity()
93
//session.getPersistenceContext().removeDatabaseSnapshot(key);
94

95         new Cascade(CascadingAction.EVICT, Cascade.AFTER_EVICT, session)
96                 .cascade(persister, object);
97     }
98 }
99
Popular Tags