KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > persister > collection > NamedQueryCollectionInitializer


1 //$Id: NamedQueryCollectionInitializer.java,v 1.2 2005/07/18 04:14:03 oneovthafew Exp $
2
package org.hibernate.persister.collection;
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.FlushMode;
9 import org.hibernate.HibernateException;
10 import org.hibernate.engine.SessionImplementor;
11 import org.hibernate.impl.AbstractQueryImpl;
12 import org.hibernate.loader.collection.CollectionInitializer;
13
14 /**
15  * A wrapper around a named query.
16  * @author Gavin King
17  */

18 public final class NamedQueryCollectionInitializer implements CollectionInitializer {
19     private final String JavaDoc queryName;
20     private final CollectionPersister persister;
21     
22     private static final Log log = LogFactory.getLog(NamedQueryCollectionInitializer.class);
23
24     public NamedQueryCollectionInitializer(String JavaDoc queryName, CollectionPersister persister) {
25         super();
26         this.queryName = queryName;
27         this.persister = persister;
28     }
29
30     public void initialize(Serializable JavaDoc key, SessionImplementor session)
31     throws HibernateException {
32         
33         if ( log.isDebugEnabled() ) {
34             log.debug(
35                     "initializing collection: " +
36                     persister.getRole() +
37                     " using named query: " +
38                     queryName
39                 );
40         }
41         
42         //TODO: is there a more elegant way than downcasting?
43
AbstractQueryImpl query = (AbstractQueryImpl) session.getNamedSQLQuery(queryName);
44         if ( query.getNamedParameters().length>0 ) {
45             query.setParameter(
46                     query.getNamedParameters()[0],
47                     key,
48                     persister.getKeyType()
49                 );
50         }
51         else {
52             query.setParameter( 0, key, persister.getKeyType() );
53         }
54         query.setCollectionKey(key)
55                 .setFlushMode(FlushMode.NEVER)
56                 .list();
57
58     }
59 }
Popular Tags