1 package org.hibernate.persister.collection; 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.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 18 public final class NamedQueryCollectionInitializer implements CollectionInitializer { 19 private final String queryName; 20 private final CollectionPersister persister; 21 22 private static final Log log = LogFactory.getLog(NamedQueryCollectionInitializer.class); 23 24 public NamedQueryCollectionInitializer(String queryName, CollectionPersister persister) { 25 super(); 26 this.queryName = queryName; 27 this.persister = persister; 28 } 29 30 public void initialize(Serializable 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 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 |