KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > impl > CriteriaImpl


1 //$Id: CriteriaImpl.java,v 1.30 2005/07/04 02:40:29 oneovthafew Exp $
2
package org.hibernate.impl;
3
4 import java.io.Serializable JavaDoc;
5 import java.util.ArrayList JavaDoc;
6 import java.util.HashMap JavaDoc;
7 import java.util.Iterator JavaDoc;
8 import java.util.List JavaDoc;
9 import java.util.Map JavaDoc;
10
11 import org.hibernate.CacheMode;
12 import org.hibernate.Criteria;
13 import org.hibernate.FetchMode;
14 import org.hibernate.FlushMode;
15 import org.hibernate.HibernateException;
16 import org.hibernate.LockMode;
17 import org.hibernate.ScrollMode;
18 import org.hibernate.ScrollableResults;
19 import org.hibernate.criterion.Criterion;
20 import org.hibernate.criterion.NaturalIdentifier;
21 import org.hibernate.criterion.Order;
22 import org.hibernate.criterion.Projection;
23 import org.hibernate.engine.SessionImplementor;
24 import org.hibernate.transform.ResultTransformer;
25 import org.hibernate.util.StringHelper;
26
27 /**
28  * Implementation of the <tt>Criteria</tt> interface
29  * @author Gavin King
30  */

31 public class CriteriaImpl implements Criteria, Serializable JavaDoc {
32
33     private FlushMode flushMode;
34     private CacheMode cacheMode;
35     private FlushMode sessionFlushMode;
36     private CacheMode sessionCacheMode;
37     private List JavaDoc criterionEntries = new ArrayList JavaDoc();
38     private List JavaDoc orderEntries = new ArrayList JavaDoc();
39     private Projection projection;
40     private Criteria projectionCriteria;
41     private Map JavaDoc fetchModes = new HashMap JavaDoc();
42     private List JavaDoc subcriteriaList = new ArrayList JavaDoc();
43     private final String JavaDoc entityOrClassName;
44     private Map JavaDoc lockModes = new HashMap JavaDoc();
45     private Integer JavaDoc maxResults;
46     private Integer JavaDoc firstResult;
47     private Integer JavaDoc timeout;
48     private Integer JavaDoc fetchSize;
49     private boolean cacheable;
50     private String JavaDoc cacheRegion;
51     private String JavaDoc comment;
52     private transient SessionImplementor session;
53     private final String JavaDoc rootAlias;
54     
55     private ResultTransformer resultTransformer = Criteria.ROOT_ENTITY;
56     
57     public void setSession(SessionImpl session) {
58         this.session = session;
59     }
60     
61     public SessionImplementor getSession() {
62         return session;
63     }
64
65     public Iterator JavaDoc iterateSubcriteria() {
66         return subcriteriaList.iterator();
67     }
68     
69     public final class Subcriteria implements Criteria, Serializable JavaDoc {
70
71         private String JavaDoc alias;
72         private String JavaDoc path;
73         private Criteria parent;
74         private LockMode lockMode;
75         
76         public Criteria getParent() {
77             return parent;
78         }
79         
80         public String JavaDoc getPath() {
81             return path;
82         }
83         
84         public String JavaDoc toString() {
85             return "Subcriteria(" +
86                 path + ":" +
87                 (alias==null ? "" : alias) +
88                 ')';
89         }
90
91         private Subcriteria(Criteria parent, String JavaDoc path, String JavaDoc alias) {
92             this.alias = alias;
93             this.path = path;
94             this.parent = parent;
95             CriteriaImpl.this.subcriteriaList.add(this);
96         }
97
98         private Subcriteria(Criteria parent, String JavaDoc path) {
99             this(parent, path, null);
100         }
101
102         public Criteria setAlias(String JavaDoc alias) {
103             this.alias = alias;
104             return this;
105         }
106         
107         public String JavaDoc getAlias() {
108             return alias;
109         }
110         
111         public Criteria add(Criterion expression) {
112             CriteriaImpl.this.add(this, expression);
113             return this;
114         }
115
116         public Criteria createAlias(String JavaDoc associationPath, String JavaDoc alias)
117             throws HibernateException {
118             new Subcriteria(this, associationPath, alias);
119             return this;
120         }
121
122         public Criteria addOrder(Order order) {
123             CriteriaImpl.this.orderEntries.add( new OrderEntry(order, this) );
124             return this;
125         }
126
127         public Criteria setCacheable(boolean cacheable) {
128             CriteriaImpl.this.setCacheable(cacheable);
129             return this;
130         }
131
132         public Criteria setCacheRegion(String JavaDoc cacheRegion) {
133             CriteriaImpl.this.setCacheRegion(cacheRegion);
134             return this;
135         }
136
137         public Criteria createCriteria(String JavaDoc associationPath)
138             throws HibernateException {
139             return new Subcriteria(Subcriteria.this, associationPath);
140         }
141
142         public List JavaDoc list() throws HibernateException {
143             return CriteriaImpl.this.list();
144         }
145
146         public ScrollableResults scroll() throws HibernateException {
147             return CriteriaImpl.this.scroll();
148         }
149
150         public ScrollableResults scroll(ScrollMode scrollMode) throws HibernateException {
151             return CriteriaImpl.this.scroll(scrollMode);
152         }
153
154         public Object JavaDoc uniqueResult() throws HibernateException {
155             return CriteriaImpl.this.uniqueResult();
156         }
157
158         public Criteria setFetchMode(String JavaDoc associationPath, FetchMode mode)
159             throws HibernateException {
160             CriteriaImpl.this.setFetchMode( StringHelper.qualify(path, associationPath), mode);
161             return this;
162         }
163
164         public Criteria setFlushMode(FlushMode flushMode) {
165             CriteriaImpl.this.setFlushMode(flushMode);
166             return this;
167         }
168         
169         public Criteria setCacheMode(CacheMode cacheMode) {
170             CriteriaImpl.this.setCacheMode(cacheMode);
171             return this;
172         }
173         
174         public Criteria setFirstResult(int firstResult) {
175             CriteriaImpl.this.setFirstResult(firstResult);
176             return this;
177         }
178
179         public Criteria setMaxResults(int maxResults) {
180             CriteriaImpl.this.setMaxResults(maxResults);
181             return this;
182         }
183
184         public Criteria setTimeout(int timeout) {
185             CriteriaImpl.this.setTimeout(timeout);
186             return this;
187         }
188
189         public Criteria setFetchSize(int fetchSize) {
190             CriteriaImpl.this.setFetchSize(fetchSize);
191             return this;
192         }
193
194         public Criteria createCriteria(String JavaDoc associationPath, String JavaDoc alias)
195             throws HibernateException {
196             return new Subcriteria(Subcriteria.this, associationPath, alias);
197         }
198         
199         public Criteria setLockMode(LockMode lockMode) {
200             this.lockMode = lockMode;
201             return this;
202         }
203         
204         public LockMode getLockMode() {
205             return lockMode;
206         }
207
208         public Criteria setLockMode(String JavaDoc alias, LockMode lockMode) {
209             CriteriaImpl.this.setLockMode(alias, lockMode);
210             return this;
211         }
212
213         public Criteria setResultTransformer(ResultTransformer resultProcessor) {
214             CriteriaImpl.this.setResultTransformer(resultProcessor);
215             return this;
216         }
217
218         public Criteria setComment(String JavaDoc comment) {
219             CriteriaImpl.this.setComment(comment);
220             return this;
221         }
222
223         public Criteria setProjection(Projection projection) {
224             CriteriaImpl.this.projection = projection;
225             CriteriaImpl.this.projectionCriteria = this;
226             setResultTransformer(PROJECTION);
227             return this;
228         }
229     }
230
231     public Criteria setFlushMode(FlushMode flushMode) {
232         this.flushMode = flushMode;
233         return this;
234     }
235     
236     public Criteria setCacheMode(CacheMode cacheMode) {
237         this.cacheMode = cacheMode;
238         return this;
239     }
240     
241     public Criteria setMaxResults(int maxResults) {
242         this.maxResults = new Integer JavaDoc(maxResults);
243         return this;
244     }
245
246     public Criteria setFirstResult(int firstResult) {
247         this.firstResult = new Integer JavaDoc(firstResult);
248         return this;
249     }
250
251     public Integer JavaDoc getFetchSize() {
252         return fetchSize;
253     }
254
255     public Criteria setFetchSize(int fetchSize) {
256         this.fetchSize = new Integer JavaDoc(fetchSize);
257         return this;
258     }
259
260     public Criteria setTimeout(int timeout) {
261         this.timeout = new Integer JavaDoc(timeout);
262         return this;
263     }
264
265     public Criteria add(Criterion expression) {
266         add(this, expression);
267         return this;
268     }
269
270     public String JavaDoc getAlias() {
271         return rootAlias;
272     }
273     
274     public Integer JavaDoc getMaxResults() {
275         return maxResults;
276     }
277     public Integer JavaDoc getFirstResult() {
278         return firstResult;
279     }
280     public Integer JavaDoc getTimeout() {
281         return timeout;
282     }
283
284     public CriteriaImpl(String JavaDoc entityOrClassName, SessionImplementor session) {
285         this(entityOrClassName, ROOT_ALIAS, session);
286     }
287
288     public CriteriaImpl(String JavaDoc entityOrClassName, String JavaDoc alias, SessionImplementor session) {
289         this.session = session;
290         this.entityOrClassName = entityOrClassName;
291         this.cacheable = false;
292         this.rootAlias = alias;
293     }
294
295     public List JavaDoc list() throws HibernateException {
296         before();
297         try {
298             return session.list(this);
299         }
300         finally {
301             after();
302         }
303     }
304     
305     public ScrollableResults scroll() {
306         before();
307         try {
308             return session.scroll(this, ScrollMode.SCROLL_INSENSITIVE);
309         }
310         finally {
311             after();
312         }
313     }
314
315     public ScrollableResults scroll(ScrollMode scrollMode) {
316         before();
317         try {
318             return session.scroll(this, scrollMode);
319         }
320         finally {
321             after();
322         }
323     }
324
325     public boolean getCacheable() {
326         return this.cacheable;
327     }
328
329     public String JavaDoc getCacheRegion() {
330         return this.cacheRegion;
331     }
332
333     public Criteria setCacheable(boolean cacheable) {
334         this.cacheable = cacheable;
335         return this;
336     }
337
338     public Criteria setCacheRegion(String JavaDoc cacheRegion) {
339         this.cacheRegion = cacheRegion.trim();
340         return this;
341     }
342
343     public Iterator JavaDoc iterateExpressionEntries() {
344         return criterionEntries.iterator();
345     }
346
347     public Iterator JavaDoc iterateOrderings() {
348         return orderEntries.iterator();
349     }
350
351     public String JavaDoc toString() {
352         return "CriteriaImpl(" +
353             entityOrClassName + ":" +
354             (rootAlias==null ? "" : rootAlias) +
355             subcriteriaList.toString() +
356             criterionEntries.toString() +
357             ( projection==null ? "" : projection.toString() ) +
358             ')';
359     }
360
361     public Criteria addOrder(Order ordering) {
362         orderEntries.add( new OrderEntry(ordering, this) );
363         return this;
364     }
365
366     public FetchMode getFetchMode(String JavaDoc path) {
367         return (FetchMode) fetchModes.get(path);
368     }
369
370     public Criteria setFetchMode(String JavaDoc associationPath, FetchMode mode) {
371         fetchModes.put(associationPath, mode);
372         return this;
373     }
374
375     public Criteria createAlias(String JavaDoc associationPath, String JavaDoc alias)
376     throws HibernateException {
377         new Subcriteria(this, associationPath, alias);
378         return this;
379     }
380
381     public Criteria add(Criteria criteriaInst, Criterion expression) {
382         criterionEntries.add( new CriterionEntry(expression, criteriaInst) );
383         return this;
384     }
385     
386     public static final class CriterionEntry implements Serializable JavaDoc {
387         private final Criterion criterion;
388         private final Criteria criteria;
389
390         private CriterionEntry(Criterion criterion, Criteria criteria) {
391             this.criteria = criteria;
392             this.criterion = criterion;
393         }
394
395         public Criterion getCriterion() {
396             return criterion;
397         }
398
399         public Criteria getCriteria() {
400             return criteria;
401         }
402
403         public String JavaDoc toString() {
404             return criterion.toString();
405         }
406     }
407
408     public static final class OrderEntry implements Serializable JavaDoc {
409         private final Order order;
410         private final Criteria criteria;
411
412         private OrderEntry(Order order, Criteria criteria) {
413             this.criteria = criteria;
414             this.order = order;
415         }
416
417         public Order getOrder() {
418             return order;
419         }
420
421         public Criteria getCriteria() {
422             return criteria;
423         }
424
425         public String JavaDoc toString() {
426             return order.toString();
427         }
428     }
429
430     public Object JavaDoc uniqueResult() throws HibernateException {
431         return AbstractQueryImpl.uniqueElement( list() );
432     }
433     
434     public String JavaDoc getEntityOrClassName() {
435         return entityOrClassName;
436     }
437
438     public Criteria createCriteria(String JavaDoc associationPath, String JavaDoc alias)
439         throws HibernateException {
440         return new Subcriteria(this, associationPath, alias);
441     }
442
443     public Criteria createCriteria(String JavaDoc associationPath)
444     throws HibernateException {
445         return new Subcriteria(this, associationPath);
446     }
447
448     public Criteria setLockMode(LockMode lockMode) {
449         return setLockMode( getAlias(), lockMode );
450     }
451
452     public Criteria setLockMode(String JavaDoc alias, LockMode lockMode) {
453         lockModes.put(alias, lockMode);
454         return this;
455     }
456
457     public Map JavaDoc getLockModes() {
458         return lockModes;
459     }
460
461     public ResultTransformer getResultTransformer() {
462         return resultTransformer;
463     }
464
465     public Criteria setResultTransformer(ResultTransformer tupleMapper) {
466         this.resultTransformer = tupleMapper;
467         return this;
468     }
469
470     public Criteria setComment(String JavaDoc comment) {
471         this.comment = comment;
472         return this;
473     }
474
475     public String JavaDoc getComment() {
476         return comment;
477     }
478
479     public Criteria setProjection(Projection projection) {
480         this.projection = projection;
481         this.projectionCriteria = this;
482         setResultTransformer(PROJECTION);
483         return this;
484     }
485     
486     public Projection getProjection() {
487         return projection;
488     }
489     
490     public Criteria getProjectionCriteria() {
491         return projectionCriteria;
492     }
493
494     protected void before() {
495         if ( flushMode!=null ) {
496             sessionFlushMode = getSession().getFlushMode();
497             getSession().setFlushMode(flushMode);
498         }
499         if ( cacheMode!=null ) {
500             sessionCacheMode = getSession().getCacheMode();
501             getSession().setCacheMode(cacheMode);
502         }
503     }
504     
505     protected void after() {
506         if (sessionFlushMode!=null) {
507             getSession().setFlushMode(sessionFlushMode);
508             sessionFlushMode = null;
509         }
510         if (sessionCacheMode!=null) {
511             getSession().setCacheMode(sessionCacheMode);
512             sessionCacheMode = null;
513         }
514     }
515     
516     public boolean isLookupByNaturalKey() {
517         if (projection!=null) return false;
518         if ( subcriteriaList.size()>0 ) return false;
519         if ( criterionEntries.size()!=1 ) return false;
520         CriterionEntry ce = (CriterionEntry) criterionEntries.get(0);
521         return ce.getCriterion() instanceof NaturalIdentifier;
522     }
523 }
524
Popular Tags