KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: CriteriaJoinWalker.java,v 1.4 2005/06/19 16:44:28 oneovthafew Exp $
2
package org.hibernate.loader.criteria;
3
4 import java.util.ArrayList JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.Map JavaDoc;
7 import java.util.Set JavaDoc;
8
9 import org.hibernate.Criteria;
10 import org.hibernate.FetchMode;
11 import org.hibernate.HibernateException;
12 import org.hibernate.LockMode;
13 import org.hibernate.MappingException;
14 import org.hibernate.engine.SessionFactoryImplementor;
15 import org.hibernate.impl.CriteriaImpl;
16 import org.hibernate.loader.AbstractEntityJoinWalker;
17 import org.hibernate.persister.entity.Joinable;
18 import org.hibernate.persister.entity.OuterJoinLoadable;
19 import org.hibernate.persister.entity.Queryable;
20 import org.hibernate.sql.JoinFragment;
21 import org.hibernate.type.AssociationType;
22 import org.hibernate.type.Type;
23 import org.hibernate.type.TypeFactory;
24 import org.hibernate.util.ArrayHelper;
25
26 /**
27  * A <tt>JoinWalker</tt> for <tt>Criteria</tt> queries.
28  *
29  * @see CriteriaLoader
30  * @author Gavin King
31  */

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

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

41     private final CriteriaQueryTranslator translator;
42     private final Set JavaDoc querySpaces;
43     private final Type[] resultTypes;
44     //the user visible aliases, which are unknown to the superclass,
45
//these are not the actual "physical" SQL aliases
46
private final String JavaDoc[] userAliases;
47     private final List JavaDoc userAliasList = new ArrayList JavaDoc();
48
49     public Type[] getResultTypes() {
50         return resultTypes;
51     }
52
53     public String JavaDoc[] getUserAliases() {
54         return userAliases;
55     }
56
57     public CriteriaJoinWalker(
58             final OuterJoinLoadable persister,
59             final CriteriaQueryTranslator translator,
60             final SessionFactoryImplementor factory,
61             final CriteriaImpl criteria,
62             final String JavaDoc rootEntityName,
63             final Map JavaDoc enabledFilters)
64     throws HibernateException {
65         super(persister, factory, enabledFilters);
66
67         this.translator = translator;
68
69         querySpaces = translator.getQuerySpaces();
70
71         if ( translator.hasProjection() ) {
72             resultTypes = translator.getProjectedTypes();
73             
74             initProjection(
75                     translator.getSelect(),
76                     translator.getWhereCondition(),
77                     translator.getOrderBy(),
78                     translator.getGroupBy(),
79                     LockMode.NONE
80                 );
81         }
82         else {
83             resultTypes = new Type[] { TypeFactory.manyToOne( persister.getEntityName() ) };
84
85             initAll( translator.getWhereCondition(), translator.getOrderBy(), LockMode.NONE );
86         }
87         
88         userAliasList.add( criteria.getAlias() ); //root entity comes *last*
89
userAliases = ArrayHelper.toStringArray(userAliasList);
90
91     }
92
93     protected int getJoinType(
94             AssociationType type,
95             FetchMode config,
96             String JavaDoc path,
97             String JavaDoc lhsTable,
98             String JavaDoc[] lhsColumns,
99             boolean nullable,
100             int currentDepth)
101     throws MappingException {
102
103         if ( translator.isJoin(path) ) {
104             return JoinFragment.INNER_JOIN;
105         }
106         else {
107             if ( translator.hasProjection() ) {
108                 return -1;
109             }
110             else {
111                 FetchMode fetchMode = translator.getRootCriteria()
112                     .getFetchMode(path);
113                 if ( isDefaultFetchMode(fetchMode) ) {
114                     return super.getJoinType(
115                             type,
116                             config,
117                             path,
118                             lhsTable,
119                             lhsColumns,
120                             nullable,
121                             currentDepth
122                         );
123                 }
124                 else {
125                     if ( fetchMode==FetchMode.JOIN ) {
126                         isDuplicateAssociation(lhsTable, lhsColumns, type); //deliberately ignore return value!
127
return getJoinType(nullable, currentDepth);
128                     }
129                     else {
130                         return -1;
131                     }
132                 }
133             }
134         }
135     }
136     
137     private static boolean isDefaultFetchMode(FetchMode fetchMode) {
138         return fetchMode==null || fetchMode==FetchMode.DEFAULT;
139     }
140
141     /**
142      * Use the discriminator, to narrow the select to instances
143      * of the queried subclass, also applying any filters.
144      */

145     protected String JavaDoc getWhereFragment() throws MappingException {
146         return super.getWhereFragment() +
147             ( (Queryable) getPersister() ).filterFragment( getAlias(), getEnabledFilters() );
148     }
149     
150     protected String JavaDoc generateTableAlias(int n, String JavaDoc path, Joinable joinable) {
151         if ( joinable.consumesEntityAlias() ) {
152             final Criteria subcriteria = translator.getCriteria(path);
153             String JavaDoc sqlAlias = subcriteria==null ? null : translator.getSQLAlias(subcriteria);
154             if (sqlAlias!=null) {
155                 userAliasList.add( subcriteria.getAlias() ); //alias may be null
156
return sqlAlias; //EARLY EXIT
157
}
158             else {
159                 userAliasList.add(null);
160             }
161         }
162         return super.generateTableAlias( n + translator.getSQLAliasCount(), path, joinable );
163     }
164
165     protected String JavaDoc generateRootAlias(String JavaDoc tableName) {
166         return CriteriaQueryTranslator.ROOT_SQL_ALIAS;
167     }
168
169     public Set JavaDoc getQuerySpaces() {
170         return querySpaces;
171     }
172     
173     public String JavaDoc getComment() {
174         return "criteria query";
175     }
176
177 }
178
Popular Tags