KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > loader > criteria > CriteriaLoader


1 //$Id: CriteriaLoader.java,v 1.8 2005/06/13 20:10:20 oneovthafew Exp $
2
package org.hibernate.loader.criteria;
3
4 import java.sql.ResultSet JavaDoc;
5 import java.sql.SQLException JavaDoc;
6 import java.util.HashMap JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Map JavaDoc;
9 import java.util.Set JavaDoc;
10
11 import org.hibernate.HibernateException;
12 import org.hibernate.LockMode;
13 import org.hibernate.QueryException;
14 import org.hibernate.ScrollMode;
15 import org.hibernate.ScrollableResults;
16 import org.hibernate.dialect.Dialect;
17 import org.hibernate.engine.QueryParameters;
18 import org.hibernate.engine.SessionFactoryImplementor;
19 import org.hibernate.engine.SessionImplementor;
20 import org.hibernate.impl.CriteriaImpl;
21 import org.hibernate.loader.OuterJoinLoader;
22 import org.hibernate.persister.entity.Loadable;
23 import org.hibernate.persister.entity.OuterJoinLoadable;
24 import org.hibernate.sql.ForUpdateFragment;
25 import org.hibernate.type.Type;
26
27 /**
28  * A <tt>Loader</tt> for <tt>Criteria</tt> queries. Note that criteria queries are
29  * more like multi-object <tt>load()</tt>s than like HQL queries.
30  *
31  * @author Gavin King
32  */

33 public class CriteriaLoader extends OuterJoinLoader {
34
35     //TODO: this class depends directly upon CriteriaImpl,
36
// in the impl package ... add a CriteriaImplementor
37
// interface
38

39     //NOTE: unlike all other Loaders, this one is NOT
40
// multithreaded, or cacheable!!
41

42     private final CriteriaQueryTranslator translator;
43     private final Set JavaDoc querySpaces;
44     private final Type[] resultTypes;
45     //the user visible aliases, which are unknown to the superclass,
46
//these are not the actual "physical" SQL aliases
47
private final String JavaDoc[] userAliases;
48
49     public CriteriaLoader(
50             final OuterJoinLoadable persister,
51             final SessionFactoryImplementor factory,
52             final CriteriaImpl criteria,
53             final String JavaDoc rootEntityName,
54             final Map JavaDoc enabledFilters)
55     throws HibernateException {
56         super(factory, enabledFilters);
57
58         translator = new CriteriaQueryTranslator(
59                 factory,
60                 criteria,
61                 rootEntityName,
62                 CriteriaQueryTranslator.ROOT_SQL_ALIAS
63         );
64
65         querySpaces = translator.getQuerySpaces();
66         
67         CriteriaJoinWalker walker = new CriteriaJoinWalker(
68                 persister,
69                 translator,
70                 factory,
71                 criteria,
72                 rootEntityName,
73                 enabledFilters
74             );
75
76         initFromWalker(walker);
77         
78         userAliases = walker.getUserAliases();
79         resultTypes = walker.getResultTypes();
80
81         postInstantiate();
82
83     }
84     
85     public ScrollableResults scroll(SessionImplementor session, ScrollMode scrollMode)
86     throws HibernateException {
87         QueryParameters qp = translator.getQueryParameters();
88         qp.setScrollMode(scrollMode);
89         return scroll(qp, resultTypes, null, session);
90     }
91
92     public List JavaDoc list(SessionImplementor session)
93     throws HibernateException {
94         return list( session, translator.getQueryParameters(), querySpaces, resultTypes );
95     }
96
97     protected Object JavaDoc getResultColumnOrRow(Object JavaDoc[] row, ResultSet JavaDoc rs, SessionImplementor session)
98     throws SQLException JavaDoc, HibernateException {
99         final Object JavaDoc[] result;
100         final String JavaDoc[] aliases;
101         if ( translator.hasProjection() ) {
102             Type[] types = translator.getProjectedTypes();
103             result = new Object JavaDoc[types.length];
104             String JavaDoc[] columnAliases = translator.getProjectedColumnAliases();
105             for ( int i=0; i<result.length; i++ ) {
106                 result[i] = types[i].nullSafeGet(rs, columnAliases[i], session, null);
107             }
108             aliases = translator.getProjectedAliases();
109         }
110         else {
111             result = row;
112             aliases = userAliases;
113         }
114         return translator.getRootCriteria().getResultTransformer().transformTuple(result, aliases);
115     }
116
117     public Set JavaDoc getQuerySpaces() {
118         return querySpaces;
119     }
120
121     protected String JavaDoc applyLocks(String JavaDoc sqlSelectString, Map JavaDoc lockModes, Dialect dialect)
122     throws QueryException {
123         
124         if ( lockModes==null || lockModes.size()==0 ) {
125             return sqlSelectString;
126         }
127         else {
128             Map JavaDoc keyColumnNames = null;
129             Loadable[] persisters = getEntityPersisters();
130             String JavaDoc[] entityAliases = getAliases();
131             if ( dialect.forUpdateOfColumns() ) {
132                 keyColumnNames = new HashMap JavaDoc();
133                 for ( int i=0; i<entityAliases.length; i++ ) {
134                     keyColumnNames.put( entityAliases[i], persisters[i].getIdentifierColumnNames() );
135                 }
136             }
137             return sqlSelectString +
138                 new ForUpdateFragment(dialect, lockModes, keyColumnNames).toFragmentString();
139         }
140     }
141
142     protected LockMode[] getLockModes(Map JavaDoc lockModes) {
143         final String JavaDoc[] entityAliases = getAliases();
144         if (entityAliases==null) return null;
145         final int size = entityAliases.length;
146         LockMode[] lockModesArray = new LockMode[size];
147         for ( int i=0; i<size; i++ ) {
148             LockMode lockMode = (LockMode) lockModes.get( entityAliases[i] );
149             lockModesArray[i] = lockMode==null ? LockMode.NONE : lockMode;
150         }
151         return lockModesArray;
152     }
153
154     protected boolean isSubselectLoadingEnabled() {
155         return hasSubselectLoadableCollections();
156     }
157     
158     protected List JavaDoc getResultList(List JavaDoc results) {
159         return translator.getRootCriteria().getResultTransformer().transformList(results);
160     }
161
162 }
163
Popular Tags