KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: EvictVisitor.java,v 1.5 2005/05/27 03:53:59 oneovthafew Exp $
2
package org.hibernate.event.def;
3
4 import org.apache.commons.logging.Log;
5 import org.apache.commons.logging.LogFactory;
6 import org.hibernate.HibernateException;
7 import org.hibernate.collection.PersistentCollection;
8 import org.hibernate.engine.CollectionEntry;
9 import org.hibernate.engine.CollectionKey;
10 import org.hibernate.event.EventSource;
11 import org.hibernate.pretty.MessageHelper;
12 import org.hibernate.type.CollectionType;
13
14 /**
15  * Evict any collections referenced by the object from the session cache.
16  * This will NOT pick up any collections that were dereferenced, so they
17  * will be deleted (suboptimal but not exactly incorrect).
18  *
19  * @author Gavin King
20  */

21 public class EvictVisitor extends AbstractVisitor {
22     
23     private static final Log log = LogFactory.getLog(EvictVisitor.class);
24
25     EvictVisitor(EventSource session) {
26         super(session);
27     }
28
29     Object JavaDoc processCollection(Object JavaDoc collection, CollectionType type)
30         throws HibernateException {
31
32         if (collection!=null) evictCollection(collection, type);
33
34         return null;
35     }
36     public void evictCollection(Object JavaDoc value, CollectionType type) {
37
38         final Object JavaDoc pc;
39         if ( type.hasHolder( getSession().getEntityMode() ) ) {
40             pc = getSession().getPersistenceContext().removeCollectionHolder(value);
41         }
42         else if ( value instanceof PersistentCollection ) {
43             pc = value;
44         }
45         else {
46             return; //EARLY EXIT!
47
}
48
49         PersistentCollection collection = (PersistentCollection) pc;
50         if ( collection.unsetSession( getSession() ) ) evictCollection(collection);
51     }
52
53     private void evictCollection(PersistentCollection collection) {
54         CollectionEntry ce = (CollectionEntry) getSession().getPersistenceContext().getCollectionEntries().remove(collection);
55         if ( log.isDebugEnabled() )
56             log.debug(
57                     "evicting collection: " +
58                     MessageHelper.collectionInfoString( ce.getLoadedPersister(), ce.getLoadedKey(), getSession().getFactory() )
59             );
60         if ( ce.getLoadedPersister() != null && ce.getLoadedKey() != null ) {
61             //TODO: is this 100% correct?
62
getSession().getPersistenceContext().getCollectionsByKey().remove(
63                     new CollectionKey( ce.getLoadedPersister(), ce.getLoadedKey(), getSession().getEntityMode() )
64             );
65         }
66     }
67
68 }
69
Popular Tags