KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: DefaultInitializeCollectionEventListener.java,v 1.8 2005/06/19 03:48:49 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.cache.CacheKey;
10 import org.hibernate.cache.entry.CollectionCacheEntry;
11 import org.hibernate.collection.PersistentCollection;
12 import org.hibernate.engine.CollectionEntry;
13 import org.hibernate.engine.PersistenceContext;
14 import org.hibernate.engine.SessionFactoryImplementor;
15 import org.hibernate.event.InitializeCollectionEvent;
16 import org.hibernate.event.InitializeCollectionEventListener;
17 import org.hibernate.engine.SessionImplementor;
18 import org.hibernate.persister.collection.CollectionPersister;
19 import org.hibernate.pretty.MessageHelper;
20
21 /**
22  * @author Gavin King
23  */

24 public class DefaultInitializeCollectionEventListener
25     extends AbstractEventListener
26     implements InitializeCollectionEventListener {
27
28     private static final Log log = LogFactory.getLog(DefaultInitializeCollectionEventListener.class);
29
30     /**
31      * called by a collection that wants to initialize itself
32      */

33     public void onInitializeCollection(InitializeCollectionEvent event)
34     throws HibernateException {
35
36         PersistentCollection collection = event.getCollection();
37         SessionImplementor source = event.getSession();
38
39         CollectionEntry ce = source.getPersistenceContext().getCollectionEntry(collection);
40         if (ce==null) throw new HibernateException("collection was evicted");
41         if ( !collection.wasInitialized() ) {
42             if ( log.isTraceEnabled() ) log.trace(
43                 "initializing collection " +
44                 MessageHelper.collectionInfoString( ce.getLoadedPersister(), ce.getLoadedKey(), source.getFactory() )
45             );
46
47             log.trace("checking second-level cache");
48             final boolean foundInCache = initializeCollectionFromCache(
49                 ce.getLoadedKey(),
50                 ce.getLoadedPersister(),
51                 collection,
52                 source
53             );
54
55             if (foundInCache) {
56                 log.trace("collection initialized from cache");
57             }
58             else {
59                 log.trace("collection not cached");
60                 ce.getLoadedPersister().initialize( ce.getLoadedKey(), source );
61                 log.trace("collection initialized");
62
63                 if ( source.getFactory().getStatistics().isStatisticsEnabled() ) {
64                     source.getFactory().getStatisticsImplementor().fetchCollection(
65                             ce.getLoadedPersister().getRole()
66                     );
67                 }
68             }
69         }
70     }
71
72     /**
73      * Try to initialize a collection from the cache
74      */

75     private boolean initializeCollectionFromCache(
76             Serializable JavaDoc id,
77             CollectionPersister persister,
78             PersistentCollection collection,
79             SessionImplementor source)
80     throws HibernateException {
81
82         if ( !source.getEnabledFilters().isEmpty() && persister.isAffectedByEnabledFilters( source ) ) {
83             log.trace( "disregarding cached version (if any) of collection due to enabled filters ");
84             return false;
85         }
86
87         final boolean useCache = persister.hasCache() &&
88             source.getCacheMode().isGetEnabled();
89
90         if ( !useCache ) {
91             return false;
92         }
93         else {
94             
95             final SessionFactoryImplementor factory = source.getFactory();
96
97             final CacheKey ck = new CacheKey(
98                     id,
99                     persister.getKeyType(),
100                     persister.getRole(),
101                     source.getEntityMode(),
102                     source.getFactory()
103                 );
104             Object JavaDoc ce = persister.getCache().get( ck, source.getTimestamp() );
105             
106             if ( factory.getStatistics().isStatisticsEnabled() ) {
107                 if (ce==null) {
108                     factory.getStatisticsImplementor().secondLevelCacheMiss(
109                             persister.getCache().getRegionName()
110                     );
111                 }
112                 else {
113                     factory.getStatisticsImplementor().secondLevelCacheHit(
114                             persister.getCache().getRegionName()
115                     );
116                 }
117
118                 
119             }
120             
121             if (ce==null) {
122                 return false;
123             }
124             else {
125
126                 CollectionCacheEntry cacheEntry = (CollectionCacheEntry) persister.getCacheEntryStructure()
127                         .destructure(ce, factory);
128             
129                 final PersistenceContext persistenceContext = source.getPersistenceContext();
130                 cacheEntry.assemble(
131                         collection,
132                         persister,
133                         persistenceContext.getCollectionOwner(id, persister)
134                 );
135                 persistenceContext.getCollectionEntry(collection).postInitialize(collection);
136                 //addInitializedCollection(collection, persister, id);
137
return true;
138             }
139             
140         }
141     }
142
143
144 }
145
Popular Tags