KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > criterion > DetachedCriteria


1 //$Id: DetachedCriteria.java,v 1.3 2005/02/12 07:19:13 steveebersole Exp $
2
package org.hibernate.criterion;
3
4 import java.io.Serializable JavaDoc;
5
6 import org.hibernate.Criteria;
7 import org.hibernate.FetchMode;
8 import org.hibernate.HibernateException;
9 import org.hibernate.Session;
10 import org.hibernate.impl.CriteriaImpl;
11 import org.hibernate.impl.SessionImpl;
12 import org.hibernate.transform.ResultTransformer;
13
14 /**
15  * Some applications need to create criteria queries in "detached
16  * mode", where the Hibernate session is not available. This class
17  * may be instantiated anywhere, and then a <literal>Criteria</literal>
18  * may be obtained by passing a session to
19  * <literal>getExecutableCriteria()</literal>. All methods have the
20  * same semantics and behavior as the corresponding methods of the
21  * <literal>Criteria</literal> interface.
22  *
23  * @see org.hibernate.Criteria
24  * @author Gavin King
25  */

26 public class DetachedCriteria implements CriteriaSpecification, Serializable JavaDoc {
27     
28     private final CriteriaImpl impl;
29     private final Criteria criteria;
30     
31     protected DetachedCriteria(String JavaDoc entityName) {
32         impl = new CriteriaImpl(entityName, null);
33         criteria = impl;
34     }
35     
36     protected DetachedCriteria(String JavaDoc entityName, String JavaDoc alias) {
37         impl = new CriteriaImpl(entityName, alias, null);
38         criteria = impl;
39     }
40     
41     protected DetachedCriteria(CriteriaImpl impl, Criteria criteria) {
42         this.impl = impl;
43         this.criteria = criteria;
44     }
45     
46     /**
47      * Get an executable instance of <literal>Criteria</literal>,
48      * to actually run the query.
49      */

50     public Criteria getExecutableCriteria(Session session) {
51         impl.setSession( (SessionImpl) session );
52         return impl;
53     }
54     
55     public static DetachedCriteria forEntityName(String JavaDoc entityName) {
56         return new DetachedCriteria(entityName);
57     }
58     
59     public static DetachedCriteria forEntityName(String JavaDoc entityName, String JavaDoc alias) {
60         return new DetachedCriteria(entityName, alias);
61     }
62     
63     public static DetachedCriteria forClass(Class JavaDoc clazz) {
64         return new DetachedCriteria( clazz.getName() );
65     }
66     
67     public static DetachedCriteria forClass(Class JavaDoc clazz, String JavaDoc alias) {
68         return new DetachedCriteria( clazz.getName() , alias );
69     }
70     
71     public DetachedCriteria add(Criterion criterion) {
72         criteria.add(criterion);
73         return this;
74     }
75
76     public DetachedCriteria addOrder(Order order) {
77         criteria.addOrder(order);
78         return this;
79     }
80
81     public DetachedCriteria createAlias(String JavaDoc associationPath, String JavaDoc alias)
82     throws HibernateException {
83         criteria.createAlias(associationPath, alias);
84         return this;
85     }
86
87     public DetachedCriteria createCriteria(String JavaDoc associationPath, String JavaDoc alias)
88     throws HibernateException {
89         return new DetachedCriteria( impl, criteria.createCriteria(associationPath) );
90     }
91
92     public DetachedCriteria createCriteria(String JavaDoc associationPath)
93     throws HibernateException {
94         return new DetachedCriteria( impl, criteria.createCriteria(associationPath) );
95     }
96
97     public String JavaDoc getAlias() {
98         return criteria.getAlias();
99     }
100
101     public DetachedCriteria setFetchMode(String JavaDoc associationPath, FetchMode mode)
102     throws HibernateException {
103         criteria.setFetchMode(associationPath, mode);
104         return this;
105     }
106
107     public DetachedCriteria setProjection(Projection projection) {
108         criteria.setProjection(projection);
109         return this;
110     }
111
112     public DetachedCriteria setResultTransformer(ResultTransformer resultTransformer) {
113         criteria.setResultTransformer(resultTransformer);
114         return this;
115     }
116     
117     public String JavaDoc toString() {
118         return "DetachableCriteria(" + criteria.toString() + ')';
119     }
120     
121     CriteriaImpl getCriteriaImpl() {
122         return impl;
123     }
124 }
125
Popular Tags