1 package org.hibernate.event.def; 3 4 import java.io.Serializable ; 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 30 public class DefaultEvictEventListener extends AbstractEventListener implements EvictEventListener { 31 32 private static final Log log = LogFactory.getLog(DefaultEvictEventListener.class); 33 34 40 public void onEvict(EvictEvent event) throws HibernateException { 41 EventSource source = event.getSession(); 42 final Object object = event.getObject(); 43 44 final PersistenceContext persistenceContext = source.getPersistenceContext(); 45 if ( object instanceof HibernateProxy ) { 46 47 LazyInitializer li = ( (HibernateProxy) object ).getHibernateLazyInitializer(); 48 Serializable 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 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 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 if ( persister.hasCollections() ) new EvictVisitor( session ).process( object, persister ); 88 89 95 new Cascade(CascadingAction.EVICT, Cascade.AFTER_EVICT, session) 96 .cascade(persister, object); 97 } 98 } 99 | Popular Tags |