~ubuntu-branches/ubuntu/wily/libhibernate3-java/wily-proposed

« back to all changes in this revision

Viewing changes to src/org/hibernate/persister/entity/NamedQueryLoader.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2007-10-14 14:43:34 UTC
  • Revision ID: james.westby@ubuntu.com-20071014144334-eamc8i0q10gs1aro
Tags: upstream-3.2.5
ImportĀ upstreamĀ versionĀ 3.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//$Id: NamedQueryLoader.java 10019 2006-06-15 07:50:12Z steve.ebersole@jboss.com $
 
2
package org.hibernate.persister.entity;
 
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.EntityKey;
 
11
import org.hibernate.engine.SessionImplementor;
 
12
import org.hibernate.impl.AbstractQueryImpl;
 
13
import org.hibernate.loader.entity.UniqueEntityLoader;
 
14
 
 
15
/**
 
16
 * Not really a <tt>Loader</tt>, just a wrapper around a
 
17
 * named query.
 
18
 * @author Gavin King
 
19
 */
 
20
public final class NamedQueryLoader implements UniqueEntityLoader {
 
21
        private final String queryName;
 
22
        private final EntityPersister persister;
 
23
        
 
24
        private static final Log log = LogFactory.getLog(NamedQueryLoader.class);
 
25
 
 
26
        public NamedQueryLoader(String queryName, EntityPersister persister) {
 
27
                super();
 
28
                this.queryName = queryName;
 
29
                this.persister = persister;
 
30
        }
 
31
 
 
32
        public Object load(Serializable id, Object optionalObject, SessionImplementor session) 
 
33
        throws HibernateException {
 
34
                
 
35
                if ( log.isDebugEnabled() ) {
 
36
                        log.debug(
 
37
                                        "loading entity: " + persister.getEntityName() + 
 
38
                                        " using named query: " + queryName 
 
39
                                );
 
40
                }
 
41
                
 
42
                AbstractQueryImpl query = (AbstractQueryImpl) session.getNamedQuery(queryName);
 
43
                if ( query.hasNamedParameters() ) {
 
44
                        query.setParameter( 
 
45
                                        query.getNamedParameters()[0], 
 
46
                                        id, 
 
47
                                        persister.getIdentifierType() 
 
48
                                );
 
49
                }
 
50
                else {
 
51
                        query.setParameter( 0, id, persister.getIdentifierType() );
 
52
                }
 
53
                query.setOptionalId(id);
 
54
                query.setOptionalEntityName( persister.getEntityName() );
 
55
                query.setOptionalObject(optionalObject);
 
56
                query.setFlushMode( FlushMode.MANUAL );
 
57
                query.list();
 
58
                
 
59
                // now look up the object we are really interested in!
 
60
                // (this lets us correctly handle proxies and multi-row
 
61
                // or multi-column queries)
 
62
                return session.getPersistenceContext()
 
63
                                .getEntity( new EntityKey( id, persister, session.getEntityMode() ) );
 
64
 
 
65
        }
 
66
}
 
 
b'\\ No newline at end of file'