001: //$Id: CriteriaImpl.java 9116 2006-01-23 21:21:01Z steveebersole $
002: package org.hibernate.impl;
003:
004: import java.io.Serializable;
005: import java.util.ArrayList;
006: import java.util.HashMap;
007: import java.util.Iterator;
008: import java.util.List;
009: import java.util.Map;
010:
011: import org.hibernate.CacheMode;
012: import org.hibernate.Criteria;
013: import org.hibernate.FetchMode;
014: import org.hibernate.FlushMode;
015: import org.hibernate.HibernateException;
016: import org.hibernate.LockMode;
017: import org.hibernate.ScrollMode;
018: import org.hibernate.ScrollableResults;
019: import org.hibernate.criterion.Criterion;
020: import org.hibernate.criterion.NaturalIdentifier;
021: import org.hibernate.criterion.Order;
022: import org.hibernate.criterion.Projection;
023: import org.hibernate.engine.SessionImplementor;
024: import org.hibernate.transform.ResultTransformer;
025: import org.hibernate.util.StringHelper;
026:
027: /**
028: * Implementation of the <tt>Criteria</tt> interface
029: * @author Gavin King
030: */
031: public class CriteriaImpl implements Criteria, Serializable {
032:
033: private final String entityOrClassName;
034: private transient SessionImplementor session;
035: private final String rootAlias;
036:
037: private List criterionEntries = new ArrayList();
038: private List orderEntries = new ArrayList();
039: private Projection projection;
040: private Criteria projectionCriteria;
041:
042: private List subcriteriaList = new ArrayList();
043:
044: private Map fetchModes = new HashMap();
045: private Map lockModes = new HashMap();
046:
047: private Integer maxResults;
048: private Integer firstResult;
049: private Integer timeout;
050: private Integer fetchSize;
051:
052: private boolean cacheable;
053: private String cacheRegion;
054: private String comment;
055:
056: private FlushMode flushMode;
057: private CacheMode cacheMode;
058: private FlushMode sessionFlushMode;
059: private CacheMode sessionCacheMode;
060:
061: private ResultTransformer resultTransformer = Criteria.ROOT_ENTITY;
062:
063: // Constructors ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
064:
065: public CriteriaImpl(String entityOrClassName,
066: SessionImplementor session) {
067: this (entityOrClassName, ROOT_ALIAS, session);
068: }
069:
070: public CriteriaImpl(String entityOrClassName, String alias,
071: SessionImplementor session) {
072: this .session = session;
073: this .entityOrClassName = entityOrClassName;
074: this .cacheable = false;
075: this .rootAlias = alias;
076: }
077:
078: public String toString() {
079: return "CriteriaImpl(" + entityOrClassName + ":"
080: + (rootAlias == null ? "" : rootAlias)
081: + subcriteriaList.toString()
082: + criterionEntries.toString()
083: + (projection == null ? "" : projection.toString())
084: + ')';
085: }
086:
087: // State ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
088:
089: public SessionImplementor getSession() {
090: return session;
091: }
092:
093: public void setSession(SessionImplementor session) {
094: this .session = session;
095: }
096:
097: public String getEntityOrClassName() {
098: return entityOrClassName;
099: }
100:
101: public Map getLockModes() {
102: return lockModes;
103: }
104:
105: public Criteria getProjectionCriteria() {
106: return projectionCriteria;
107: }
108:
109: public Iterator iterateSubcriteria() {
110: return subcriteriaList.iterator();
111: }
112:
113: public Iterator iterateExpressionEntries() {
114: return criterionEntries.iterator();
115: }
116:
117: public Iterator iterateOrderings() {
118: return orderEntries.iterator();
119: }
120:
121: public Criteria add(Criteria criteriaInst, Criterion expression) {
122: criterionEntries.add(new CriterionEntry(expression,
123: criteriaInst));
124: return this ;
125: }
126:
127: // Criteria impl ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
128:
129: public String getAlias() {
130: return rootAlias;
131: }
132:
133: public Projection getProjection() {
134: return projection;
135: }
136:
137: public Criteria setProjection(Projection projection) {
138: this .projection = projection;
139: this .projectionCriteria = this ;
140: setResultTransformer(PROJECTION);
141: return this ;
142: }
143:
144: public Criteria add(Criterion expression) {
145: add(this , expression);
146: return this ;
147: }
148:
149: public Criteria addOrder(Order ordering) {
150: orderEntries.add(new OrderEntry(ordering, this ));
151: return this ;
152: }
153:
154: public FetchMode getFetchMode(String path) {
155: return (FetchMode) fetchModes.get(path);
156: }
157:
158: public Criteria setFetchMode(String associationPath, FetchMode mode) {
159: fetchModes.put(associationPath, mode);
160: return this ;
161: }
162:
163: public Criteria setLockMode(LockMode lockMode) {
164: return setLockMode(getAlias(), lockMode);
165: }
166:
167: public Criteria setLockMode(String alias, LockMode lockMode) {
168: lockModes.put(alias, lockMode);
169: return this ;
170: }
171:
172: public Criteria createAlias(String associationPath, String alias) {
173: return createAlias(associationPath, alias, INNER_JOIN);
174: }
175:
176: public Criteria createAlias(String associationPath, String alias,
177: int joinType) {
178: new Subcriteria(this , associationPath, alias, joinType);
179: return this ;
180: }
181:
182: public Criteria createCriteria(String associationPath) {
183: return createCriteria(associationPath, INNER_JOIN);
184: }
185:
186: public Criteria createCriteria(String associationPath, int joinType) {
187: return new Subcriteria(this , associationPath, joinType);
188: }
189:
190: public Criteria createCriteria(String associationPath, String alias) {
191: return createCriteria(associationPath, alias, INNER_JOIN);
192: }
193:
194: public Criteria createCriteria(String associationPath,
195: String alias, int joinType) {
196: return new Subcriteria(this , associationPath, alias, joinType);
197: }
198:
199: public ResultTransformer getResultTransformer() {
200: return resultTransformer;
201: }
202:
203: public Criteria setResultTransformer(ResultTransformer tupleMapper) {
204: this .resultTransformer = tupleMapper;
205: return this ;
206: }
207:
208: public Integer getMaxResults() {
209: return maxResults;
210: }
211:
212: public Criteria setMaxResults(int maxResults) {
213: this .maxResults = new Integer(maxResults);
214: return this ;
215: }
216:
217: public Integer getFirstResult() {
218: return firstResult;
219: }
220:
221: public Criteria setFirstResult(int firstResult) {
222: this .firstResult = new Integer(firstResult);
223: return this ;
224: }
225:
226: public Integer getFetchSize() {
227: return fetchSize;
228: }
229:
230: public Criteria setFetchSize(int fetchSize) {
231: this .fetchSize = new Integer(fetchSize);
232: return this ;
233: }
234:
235: public Integer getTimeout() {
236: return timeout;
237: }
238:
239: public Criteria setTimeout(int timeout) {
240: this .timeout = new Integer(timeout);
241: return this ;
242: }
243:
244: public boolean getCacheable() {
245: return this .cacheable;
246: }
247:
248: public Criteria setCacheable(boolean cacheable) {
249: this .cacheable = cacheable;
250: return this ;
251: }
252:
253: public String getCacheRegion() {
254: return this .cacheRegion;
255: }
256:
257: public Criteria setCacheRegion(String cacheRegion) {
258: this .cacheRegion = cacheRegion.trim();
259: return this ;
260: }
261:
262: public String getComment() {
263: return comment;
264: }
265:
266: public Criteria setComment(String comment) {
267: this .comment = comment;
268: return this ;
269: }
270:
271: public Criteria setFlushMode(FlushMode flushMode) {
272: this .flushMode = flushMode;
273: return this ;
274: }
275:
276: public Criteria setCacheMode(CacheMode cacheMode) {
277: this .cacheMode = cacheMode;
278: return this ;
279: }
280:
281: public List list() throws HibernateException {
282: before();
283: try {
284: return session.list(this );
285: } finally {
286: after();
287: }
288: }
289:
290: public ScrollableResults scroll() {
291: return scroll(ScrollMode.SCROLL_INSENSITIVE);
292: }
293:
294: public ScrollableResults scroll(ScrollMode scrollMode) {
295: before();
296: try {
297: return session.scroll(this , scrollMode);
298: } finally {
299: after();
300: }
301: }
302:
303: public Object uniqueResult() throws HibernateException {
304: return AbstractQueryImpl.uniqueElement(list());
305: }
306:
307: protected void before() {
308: if (flushMode != null) {
309: sessionFlushMode = getSession().getFlushMode();
310: getSession().setFlushMode(flushMode);
311: }
312: if (cacheMode != null) {
313: sessionCacheMode = getSession().getCacheMode();
314: getSession().setCacheMode(cacheMode);
315: }
316: }
317:
318: protected void after() {
319: if (sessionFlushMode != null) {
320: getSession().setFlushMode(sessionFlushMode);
321: sessionFlushMode = null;
322: }
323: if (sessionCacheMode != null) {
324: getSession().setCacheMode(sessionCacheMode);
325: sessionCacheMode = null;
326: }
327: }
328:
329: public boolean isLookupByNaturalKey() {
330: if (projection != null) {
331: return false;
332: }
333: if (subcriteriaList.size() > 0) {
334: return false;
335: }
336: if (criterionEntries.size() != 1) {
337: return false;
338: }
339: CriterionEntry ce = (CriterionEntry) criterionEntries.get(0);
340: return ce.getCriterion() instanceof NaturalIdentifier;
341: }
342:
343: // Inner classes ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
344:
345: public final class Subcriteria implements Criteria, Serializable {
346:
347: private String alias;
348: private String path;
349: private Criteria parent;
350: private LockMode lockMode;
351: private int joinType;
352:
353: // Constructors ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
354:
355: private Subcriteria(Criteria parent, String path, String alias,
356: int joinType) {
357: this .alias = alias;
358: this .path = path;
359: this .parent = parent;
360: this .joinType = joinType;
361: CriteriaImpl.this .subcriteriaList.add(this );
362: }
363:
364: private Subcriteria(Criteria parent, String path, int joinType) {
365: this (parent, path, null, joinType);
366: }
367:
368: public String toString() {
369: return "Subcriteria(" + path + ":"
370: + (alias == null ? "" : alias) + ')';
371: }
372:
373: // State ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
374:
375: public String getAlias() {
376: return alias;
377: }
378:
379: public void setAlias(String alias) {
380: this .alias = alias;
381: }
382:
383: public String getPath() {
384: return path;
385: }
386:
387: public Criteria getParent() {
388: return parent;
389: }
390:
391: public LockMode getLockMode() {
392: return lockMode;
393: }
394:
395: public Criteria setLockMode(LockMode lockMode) {
396: this .lockMode = lockMode;
397: return this ;
398: }
399:
400: public int getJoinType() {
401: return joinType;
402: }
403:
404: // Criteria impl ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
405:
406: public Criteria add(Criterion expression) {
407: CriteriaImpl.this .add(this , expression);
408: return this ;
409: }
410:
411: public Criteria addOrder(Order order) {
412: CriteriaImpl.this .orderEntries.add(new OrderEntry(order,
413: this ));
414: return this ;
415: }
416:
417: public Criteria createAlias(String associationPath, String alias) {
418: return createAlias(associationPath, alias, INNER_JOIN);
419: }
420:
421: public Criteria createAlias(String associationPath,
422: String alias, int joinType) throws HibernateException {
423: new Subcriteria(this , associationPath, alias, joinType);
424: return this ;
425: }
426:
427: public Criteria createCriteria(String associationPath) {
428: return createCriteria(associationPath, INNER_JOIN);
429: }
430:
431: public Criteria createCriteria(String associationPath,
432: int joinType) throws HibernateException {
433: return new Subcriteria(Subcriteria.this , associationPath,
434: joinType);
435: }
436:
437: public Criteria createCriteria(String associationPath,
438: String alias) {
439: return createCriteria(associationPath, alias, INNER_JOIN);
440: }
441:
442: public Criteria createCriteria(String associationPath,
443: String alias, int joinType) throws HibernateException {
444: return new Subcriteria(Subcriteria.this , associationPath,
445: alias, joinType);
446: }
447:
448: public Criteria setCacheable(boolean cacheable) {
449: CriteriaImpl.this .setCacheable(cacheable);
450: return this ;
451: }
452:
453: public Criteria setCacheRegion(String cacheRegion) {
454: CriteriaImpl.this .setCacheRegion(cacheRegion);
455: return this ;
456: }
457:
458: public List list() throws HibernateException {
459: return CriteriaImpl.this .list();
460: }
461:
462: public ScrollableResults scroll() throws HibernateException {
463: return CriteriaImpl.this .scroll();
464: }
465:
466: public ScrollableResults scroll(ScrollMode scrollMode)
467: throws HibernateException {
468: return CriteriaImpl.this .scroll(scrollMode);
469: }
470:
471: public Object uniqueResult() throws HibernateException {
472: return CriteriaImpl.this .uniqueResult();
473: }
474:
475: public Criteria setFetchMode(String associationPath,
476: FetchMode mode) throws HibernateException {
477: CriteriaImpl.this .setFetchMode(StringHelper.qualify(path,
478: associationPath), mode);
479: return this ;
480: }
481:
482: public Criteria setFlushMode(FlushMode flushMode) {
483: CriteriaImpl.this .setFlushMode(flushMode);
484: return this ;
485: }
486:
487: public Criteria setCacheMode(CacheMode cacheMode) {
488: CriteriaImpl.this .setCacheMode(cacheMode);
489: return this ;
490: }
491:
492: public Criteria setFirstResult(int firstResult) {
493: CriteriaImpl.this .setFirstResult(firstResult);
494: return this ;
495: }
496:
497: public Criteria setMaxResults(int maxResults) {
498: CriteriaImpl.this .setMaxResults(maxResults);
499: return this ;
500: }
501:
502: public Criteria setTimeout(int timeout) {
503: CriteriaImpl.this .setTimeout(timeout);
504: return this ;
505: }
506:
507: public Criteria setFetchSize(int fetchSize) {
508: CriteriaImpl.this .setFetchSize(fetchSize);
509: return this ;
510: }
511:
512: public Criteria setLockMode(String alias, LockMode lockMode) {
513: CriteriaImpl.this .setLockMode(alias, lockMode);
514: return this ;
515: }
516:
517: public Criteria setResultTransformer(
518: ResultTransformer resultProcessor) {
519: CriteriaImpl.this .setResultTransformer(resultProcessor);
520: return this ;
521: }
522:
523: public Criteria setComment(String comment) {
524: CriteriaImpl.this .setComment(comment);
525: return this ;
526: }
527:
528: public Criteria setProjection(Projection projection) {
529: CriteriaImpl.this .projection = projection;
530: CriteriaImpl.this .projectionCriteria = this ;
531: setResultTransformer(PROJECTION);
532: return this ;
533: }
534: }
535:
536: public static final class CriterionEntry implements Serializable {
537: private final Criterion criterion;
538: private final Criteria criteria;
539:
540: private CriterionEntry(Criterion criterion, Criteria criteria) {
541: this .criteria = criteria;
542: this .criterion = criterion;
543: }
544:
545: public Criterion getCriterion() {
546: return criterion;
547: }
548:
549: public Criteria getCriteria() {
550: return criteria;
551: }
552:
553: public String toString() {
554: return criterion.toString();
555: }
556: }
557:
558: public static final class OrderEntry implements Serializable {
559: private final Order order;
560: private final Criteria criteria;
561:
562: private OrderEntry(Order order, Criteria criteria) {
563: this .criteria = criteria;
564: this .order = order;
565: }
566:
567: public Order getOrder() {
568: return order;
569: }
570:
571: public Criteria getCriteria() {
572: return criteria;
573: }
574:
575: public String toString() {
576: return order.toString();
577: }
578: }
579: }
|