KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > ejb > QueryImpl


1 //$Id: QueryImpl.java,v 1.5 2005/07/09 20:04:43 epbernard Exp $
2
package org.hibernate.ejb;
3
4 import java.util.Calendar JavaDoc;
5 import java.util.Collection JavaDoc;
6 import java.util.Date JavaDoc;
7 import java.util.List JavaDoc;
8 import javax.persistence.EntityNotFoundException;
9 import javax.persistence.FlushModeType;
10 import javax.persistence.Query;
11 import javax.persistence.TemporalType;
12
13 import org.hibernate.CacheMode;
14 import org.hibernate.FlushMode;
15 import org.hibernate.NonUniqueResultException;
16
17 import static javax.persistence.TemporalType.DATE;
18 import static javax.persistence.TemporalType.TIME;
19 import static javax.persistence.TemporalType.TIMESTAMP;
20
21 /**
22  * @author <a HREF="mailto:gavin@hibernate.org">Gavin King</a>
23  * @version $Revision: 1.5 $
24  */

25 public class QueryImpl implements Query {
26     private org.hibernate.Query query;
27
28     public QueryImpl(org.hibernate.Query query) {
29         this.query = query;
30     }
31
32     public org.hibernate.Query getHibernateQuery() {
33         return query;
34     }
35
36     public int executeUpdate() {
37         return query.executeUpdate();
38     }
39
40     public List JavaDoc getResultList() {
41         return query.list();
42     }
43
44     public Object JavaDoc getSingleResult() {
45         try {
46             Object JavaDoc result = query.uniqueResult();
47
48             if ( result == null ) {
49                 throw new EntityNotFoundException( "No entity found for query" );
50             }
51
52             return result;
53         }
54         catch (NonUniqueResultException e) {
55             throw new javax.persistence.NonUniqueResultException( e.getMessage() );
56         }
57     }
58
59     public Query setMaxResults(int maxResult) {
60         if ( maxResult < 0 ) {
61             throw new IllegalArgumentException JavaDoc(
62                     "Negative ("
63                     + maxResult
64                     + ") parameter passed in to setMaxResults"
65             );
66         }
67         query.setMaxResults( maxResult );
68         return this;
69     }
70
71     public Query setFirstResult(int firstResult) {
72         if ( firstResult < 0 ) {
73             throw new IllegalArgumentException JavaDoc(
74                     "Negative ("
75                     + firstResult
76                     + ") parameter passed in to setMaxResults"
77             );
78         }
79         query.setFirstResult( firstResult );
80         return this;
81     }
82
83     public Query setHint(String JavaDoc hintName, Object JavaDoc value) {
84         try {
85             if ( "org.hibernate.timeout".equals( hintName ) ) {
86                 query.setTimeout( (Integer JavaDoc) value );
87             }
88             else if ( "org.hibernate.comment".equals( hintName ) ) {
89                 query.setComment( (String JavaDoc) value );
90             }
91             else if ( "org.hibernate.fetchSize".equals( hintName ) ) {
92                 query.setFetchSize( (Integer JavaDoc) value );
93             }
94             else if ( "org.hibernate.cacheRegion".equals( hintName ) ) {
95                 query.setCacheRegion( (String JavaDoc) value );
96             }
97             else if ( "org.hibernate.cacheable".equals( hintName ) ) {
98                 query.setCacheable( (Boolean JavaDoc) value );
99             }
100             else if ( "org.hibernate.readOnly".equals( hintName ) ) {
101                 query.setReadOnly( (Boolean JavaDoc) value );
102             }
103             else if ( "org.hibernate.cacheMode".equals( hintName ) ) {
104                 query.setCacheMode( (CacheMode) value );
105             }
106         }
107         catch (ClassCastException JavaDoc e) {
108             throw new IllegalArgumentException JavaDoc( "Value for hint" );
109         }
110         return this;
111     }
112
113     public Query setParameter(String JavaDoc name, Object JavaDoc value) {
114         if ( value instanceof Collection JavaDoc ) {
115             query.setParameterList( name, (Collection JavaDoc) value );
116         }
117         else {
118             query.setParameter( name, value );
119         }
120         return this;
121     }
122
123     public Query setParameter(String JavaDoc name, Date JavaDoc value, TemporalType temporalType) {
124         if ( temporalType == DATE ) {
125             query.setDate( name, value );
126         }
127         else if ( temporalType == TIME ) {
128             query.setTime( name, value );
129         }
130         else if ( temporalType == TIMESTAMP ) {
131             query.setTimestamp( name, value );
132         }
133         return this;
134     }
135
136     public Query setParameter(String JavaDoc name, Calendar JavaDoc value, TemporalType temporalType) {
137         if ( temporalType == DATE ) {
138             query.setCalendarDate( name, value );
139         }
140         else if ( temporalType == TIME ) {
141             throw new IllegalArgumentException JavaDoc( "not yet implemented" );
142         }
143         else if ( temporalType == TIMESTAMP ) {
144             query.setCalendar( name, value );
145         }
146         return this;
147     }
148
149     public Query setParameter(int position, Object JavaDoc value) {
150         query.setParameter( position, value );
151         return this;
152     }
153
154     public Query setParameter(int position, Date JavaDoc value, TemporalType temporalType) {
155         if ( temporalType == DATE ) {
156             query.setDate( position, value );
157         }
158         else if ( temporalType == TIME ) {
159             query.setTime( position, value );
160         }
161         else if ( temporalType == TIMESTAMP ) {
162             query.setTimestamp( position, value );
163         }
164         return this;
165     }
166
167     public Query setParameter(int position, Calendar JavaDoc value, TemporalType temporalType) {
168         if ( temporalType == DATE ) {
169             query.setCalendarDate( position, value );
170         }
171         else if ( temporalType == TIME ) {
172             throw new IllegalArgumentException JavaDoc( "not yet implemented" );
173         }
174         else if ( temporalType == TIMESTAMP ) {
175             query.setCalendar( position, value );
176         }
177         return this;
178     }
179
180     public Query setFlushMode(FlushModeType flushMode) {
181         if ( flushMode == FlushModeType.AUTO ) {
182             query.setFlushMode( FlushMode.AUTO );
183         }
184         else if ( flushMode == FlushModeType.COMMIT ) {
185             query.setFlushMode( FlushMode.COMMIT );
186         }
187         else if ( flushMode == FlushModeType.NEVER ) {
188             query.setFlushMode( FlushMode.NEVER );
189         }
190         return this;
191     }
192 }
193
Popular Tags