KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > blandware > atleap > persistence > hibernate > core > BaseDAOHibernate


1 /*
2  * Copyright 2004 Blandware (http://www.blandware.com)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package com.blandware.atleap.persistence.hibernate.core;
17
18 import com.blandware.atleap.common.util.QueryInfo;
19 import com.blandware.atleap.model.core.BaseObject;
20 import com.blandware.atleap.persistence.core.DAO;
21 import org.hibernate.HibernateException;
22 import org.hibernate.Query;
23 import org.hibernate.Session;
24 import org.hibernate.type.Type;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27 import org.springframework.orm.hibernate3.HibernateCallback;
28 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
29
30 import java.util.List JavaDoc;
31
32
33 /**
34  * <p>This class serves as the Base class for all other DAOs - namely to hold
35  * common methods that they might all use.</p>
36  * <p><a HREF="BaseDAOHibernate.java.htm"><i>View Source</i></a></p>
37  *
38  * @author Sergey Zubtcovskii <a HREF="mailto:sergey.zubtcovskii@blandware.com">&lt;sergey.zubtcovskii@blandware.com&gt;</a>
39  * @author Andrey Grebnev <a HREF="mailto:andrey.grebnev@blandware.com">&lt;andrey.grebnev@blandware.com&gt;</a>
40  * @version $Revision: 1.19 $ $Date: 2005/10/19 07:28:01 $
41  */

42 public class BaseDAOHibernate extends HibernateDaoSupport implements DAO {
43
44     protected transient final Log log = LogFactory.getLog(getClass());
45
46     /**
47      * @see com.blandware.atleap.persistence.core.DAO#removeFromCache(com.blandware.atleap.model.core.BaseObject)
48      */

49     public void removeFromCache(BaseObject object) {
50         if (object != null)
51             getHibernateTemplate().evict(object);
52     }
53
54     /**
55      * @see com.blandware.atleap.persistence.core.DAO#reload(Object)
56      */

57     public void reload(Object JavaDoc object) {
58         getHibernateTemplate().refresh(object);
59     }
60
61     /**
62      * Executes find using <code>org.springframework.orm.hibernate3.HibernateCallback</code>
63      * with no additional query info, args, cache region; the query is not cacheable
64      *
65      * @param hql Query to execute
66      * @return List of entities
67      * @see #executeFind(String, com.blandware.atleap.common.util.QueryInfo, Object[], org.hibernate.type.Type[], boolean, String)
68      * @see org.springframework.orm.hibernate3.HibernateCallback
69      */

70     protected List JavaDoc executeFind(String JavaDoc hql) {
71         return executeFind(hql, null, null, null, false, null);
72     }
73
74
75     /**
76      * Executes find using <code>org.springframework.orm.hibernate3.HibernateCallback</code>
77      * with no additional query info and args
78      *
79      * @param hql Query to execute
80      * @param cacheable <code>true</code> if the query is cacheable
81      * @param cacheRegion region of cache. E.g. one that used in configuration file of EHCahce (ehcache.xml)
82      * @return List of entities
83      * @see #executeFind(String, com.blandware.atleap.common.util.QueryInfo, Object[], org.hibernate.type.Type[], boolean, String)
84      * @see org.springframework.orm.hibernate3.HibernateCallback
85      */

86     protected List JavaDoc executeFind(String JavaDoc hql, boolean cacheable, String JavaDoc cacheRegion) {
87         return executeFind(hql, null, null, null, cacheable, cacheRegion);
88     }
89
90     /**
91      * Executes find using <code>org.springframework.orm.hibernate3.HibernateCallback</code>
92      * with no additional query info; the query is not cacheable (so no cache
93      * region is supplied); with args, but not their types
94      *
95      * @param hql Query to execute
96      * @param args Arguments to add to query. If <code>null</code>, nothing will be added.
97      * @return List of entities
98      * @see #executeFind(String, com.blandware.atleap.common.util.QueryInfo, Object[], org.hibernate.type.Type[], boolean, String)
99      * @see org.springframework.orm.hibernate3.HibernateCallback
100      */

101     protected List JavaDoc executeFind(String JavaDoc hql, Object JavaDoc[] args) {
102         return executeFind(hql, null, args, null, false, null);
103     }
104
105     /**
106      * Executes find using <code>org.springframework.orm.hibernate3.HibernateCallback</code>
107      * with no additional query info; the query is not cacheable (so no cache
108      * region is supplied); with args and their types
109      *
110      * @param hql Query to execute
111      * @param args Arguments to add to query. If <code>null</code>, nothing will be added.
112      * @param types Types of arguments. If <code>null</code>, Hibernate will determine types by itself.
113      * @return List of entities
114      * @see #executeFind(String, com.blandware.atleap.common.util.QueryInfo, Object[], org.hibernate.type.Type[], boolean, String)
115      * @see org.springframework.orm.hibernate3.HibernateCallback
116      */

117     protected List JavaDoc executeFind(String JavaDoc hql, Object JavaDoc[] args, Type[] types) {
118         return executeFind(hql, null, args, types, false, null);
119     }
120
121     /**
122      * Executes find using <code>org.springframework.orm.hibernate3.HibernateCallback</code>
123      * with no additional query info; with args but not their types
124      *
125      * @param hql Query to execute
126      * @param args Arguments to add to query. If <code>null</code>, nothing will be added.
127      * @param cacheable <code>true</code> if the query is cacheable
128      * @param cacheRegion region of cache. E.g. one that used in configuration file of EHCahce (ehcache.xml)
129      * @return List of entities
130      * @see #executeFind(String, com.blandware.atleap.common.util.QueryInfo, Object[], org.hibernate.type.Type[], boolean, String)
131      * @see org.springframework.orm.hibernate3.HibernateCallback
132      */

133     protected List JavaDoc executeFind(String JavaDoc hql, Object JavaDoc[] args, boolean cacheable, String JavaDoc cacheRegion) {
134         return executeFind(hql, null, args, null, cacheable, cacheRegion);
135     }
136
137     /**
138      * Executes find using <code>org.springframework.orm.hibernate3.HibernateCallback</code>
139      * with no additional query info; with arguments and their types
140      *
141      * @param hql Query to execute
142      * @param args Arguments to add to query. If <code>null</code>, nothing will be added.
143      * @param types Types of arguments. If <code>null</code>, Hibernate will determine types by itself.
144      * @param cacheable <code>true</code> if the query is cacheable
145      * @param cacheRegion region of cache. E.g. one that used in configuration file of EHCahce (ehcache.xml)
146      * @return List of entities
147      * @see #executeFind(String, com.blandware.atleap.common.util.QueryInfo, Object[], org.hibernate.type.Type[], boolean, String)
148      * @see org.springframework.orm.hibernate3.HibernateCallback
149      */

150     protected List JavaDoc executeFind(String JavaDoc hql, Object JavaDoc[] args, Type[] types, boolean cacheable, String JavaDoc cacheRegion) {
151         return executeFind(hql, null, args, types, cacheable, cacheRegion);
152     }
153
154     /**
155      * Executes find using <code>org.springframework.orm.hibernate3.HibernateCallback</code>
156      * with additional query info, but without any arguments; the query is not
157      * cacheable
158      *
159      * @param hql Query to execute
160      * @param queryInfo Object with additional information for query, currently offset and limit
161      * @return List of entities
162      * @see #executeFind(String, com.blandware.atleap.common.util.QueryInfo, Object[], org.hibernate.type.Type[], boolean, String)
163      * @see org.springframework.orm.hibernate3.HibernateCallback
164      * @see com.blandware.atleap.common.util.QueryInfo
165      */

166     protected List JavaDoc executeFind(String JavaDoc hql, QueryInfo queryInfo) {
167         return executeFind(hql, queryInfo, null, null, false, null);
168     }
169
170     /**
171      * Executes find using <code>org.springframework.orm.hibernate3.HibernateCallback</code>
172      * with additional query info; with arguments but without their types; the
173      * query is not cacheable
174      *
175      * @param hql Query to execute
176      * @param queryInfo Object with additional information for query, currently offset and limit
177      * @param args Arguments to add to query. If <code>null</code>, nothing will be added.
178      * @return List of entities
179      * @see #executeFind(String, com.blandware.atleap.common.util.QueryInfo, Object[], org.hibernate.type.Type[], boolean, String)
180      * @see org.springframework.orm.hibernate3.HibernateCallback
181      * @see com.blandware.atleap.common.util.QueryInfo
182      */

183     protected List JavaDoc executeFind(String JavaDoc hql, QueryInfo queryInfo, Object JavaDoc[] args) {
184         return executeFind(hql, queryInfo, args, null, false, null);
185     }
186
187     /**
188      * Executes find using <code>org.springframework.orm.hibernate3.HibernateCallback</code>
189      * with additional query info; with arguments and their types; the query is
190      * not cacheable
191      *
192      * @param hql Query to execute
193      * @param queryInfo Object with additional information for query, currently offset and limit
194      * @param args Arguments to add to query. If <code>null</code>, nothing will be added.
195      * @param types Types of arguments. If <code>null</code>, Hibernate will determine types by itself.
196      * @return List of entities
197      * @see #executeFind(String, com.blandware.atleap.common.util.QueryInfo, Object[], org.hibernate.type.Type[], boolean, String)
198      * @see org.springframework.orm.hibernate3.HibernateCallback
199      * @see com.blandware.atleap.common.util.QueryInfo
200      */

201     protected List JavaDoc executeFind(String JavaDoc hql, QueryInfo queryInfo, Object JavaDoc[] args, Type[] types) {
202         return executeFind(hql, queryInfo, args, types, false, null);
203     }
204
205     /**
206      * Executes find using <code>org.springframework.orm.hibernate3.HibernateCallback</code>
207      * with additional query info; with arguments but without their types
208      *
209      * @param hql Query to execute
210      * @param queryInfo Object with additional information for query, currently offset and limit
211      * @param args Arguments to add to query. If <code>null</code>, nothing will be added.
212      * @param cacheable <code>true</code> if the query is cacheable
213      * @param cacheRegion region of cache. E.g. one that used in configuration file of EHCahce (ehcache.xml)
214      * @return List of entities
215      * @see #executeFind(String, com.blandware.atleap.common.util.QueryInfo, Object[], org.hibernate.type.Type[], boolean, String)
216      * @see org.springframework.orm.hibernate3.HibernateCallback
217      * @see com.blandware.atleap.common.util.QueryInfo
218      */

219     protected List JavaDoc executeFind(String JavaDoc hql, QueryInfo queryInfo, Object JavaDoc[] args, boolean cacheable, String JavaDoc cacheRegion) {
220         return executeFind(hql, queryInfo, args, null, cacheable, cacheRegion);
221     }
222
223     /**
224      * Executes find using <code>org.springframework.orm.hibernate3.HibernateCallback</code>
225      * with additional query info; with arguments and their types
226      *
227      * @param hql Query to execute
228      * @param queryInfo Object with additional information for query, currently offset and limit
229      * @param args Arguments to add to query. If <code>null</code>, nothing will be added.
230      * @param types Types of arguments. If <code>null</code>, Hibernate will determine types by itself.
231      * @param cacheable <code>true</code> if the query is cacheable
232      * @param cacheRegion region of cache. E.g. one that used in configuration file of EHCahce (ehcache.xml)
233      * @return List of found entities
234      * @see org.springframework.orm.hibernate3.HibernateCallback
235      * @see com.blandware.atleap.common.util.QueryInfo
236      */

237     protected List JavaDoc executeFind(final String JavaDoc hql, final QueryInfo queryInfo, final Object JavaDoc[] args, final Type[] types, final boolean cacheable, final String JavaDoc cacheRegion) {
238         return getHibernateTemplate().executeFind(new HibernateCallback() {
239             public Object JavaDoc doInHibernate(Session session) throws HibernateException {
240                 Query query = session.createQuery(hql);
241                 query.setCacheable(cacheable);
242                 if ( cacheRegion != null ) {
243                     query.setCacheRegion(cacheRegion);
244                 }
245                 if ( args != null ) {
246                     for ( int i = 0; i < args.length; i++ ) {
247                         Object JavaDoc arg = args[i];
248                         Type type = null;
249                         if ( types != null && i < types.length ) {
250                             type = types[i];
251                         }
252                         if ( type == null ) {
253                             query.setParameter(i, arg);
254                         } else {
255                             query.setParameter(i, arg, type);
256                         }
257                     }
258                 }
259                 if ( queryInfo != null ) {
260                     if ( queryInfo.getLimit() != null ) {
261                         query.setMaxResults(queryInfo.getLimit().intValue());
262                     }
263                     if ( queryInfo.getOffset() != null ) {
264                         query.setFirstResult(queryInfo.getOffset().intValue());
265                     }
266                 }
267                 return query.list();
268             }
269         });
270     }
271
272
273     /**
274      * Returns unique result matching specified query using <code>org.springframework.orm.hibernate3.HibernateCallback</code>
275      * with no arguments; the query is not cacheable
276      *
277      * @param hql Query to execute
278      * @return Unique result matching specified query
279      * @see #findUniqueResult(String, Object[], org.hibernate.type.Type[], boolean, String)
280      * @see org.springframework.orm.hibernate3.HibernateCallback
281      */

282     public Object JavaDoc findUniqueResult(String JavaDoc hql) {
283         return findUniqueResult(hql, null, null, false, null);
284     }
285
286     /**
287      * Returns unique result matching specified query using <code>org.springframework.orm.hibernate3.HibernateCallback</code>
288      * with no arguments
289      *
290      * @param hql Query to execute
291      * @param cacheable <code>true</code> if the query is cacheable
292      * @param cacheRegion region of cache. E.g. one that used in configuration file of EHCahce (ehcache.xml)
293      * @return Unique result matching specified query
294      * @see #findUniqueResult(String, Object[], org.hibernate.type.Type[], boolean, String)
295      * @see org.springframework.orm.hibernate3.HibernateCallback
296      */

297     public Object JavaDoc findUniqueResult(String JavaDoc hql, boolean cacheable, String JavaDoc cacheRegion) {
298         return findUniqueResult(hql, null, null, cacheable, cacheRegion);
299     }
300
301     /**
302      * Returns unique result matching specified query using <code>org.springframework.orm.hibernate3.HibernateCallback</code>
303      * with arguments but without their types; the query is not cacheable
304      *
305      * @param hql Query to execute
306      * @param args Arguments to set
307      * @return Unique result matching specified query
308      * @see #findUniqueResult(String, Object[], org.hibernate.type.Type[], boolean, String)
309      * @see org.springframework.orm.hibernate3.HibernateCallback
310      */

311     public Object JavaDoc findUniqueResult(String JavaDoc hql, Object JavaDoc[] args) {
312         return findUniqueResult(hql, args, null, false, null);
313     }
314
315     /**
316      * Returns unique result matching specified query using <code>org.springframework.orm.hibernate3.HibernateCallback</code>
317      * with arguments but without their types
318      *
319      * @param hql Query to execute
320      * @param args Arguments to set
321      * @param cacheable <code>true</code> if the query is cacheable
322      * @param cacheRegion region of cache. E.g. one that used in configuration file of EHCahce (ehcache.xml)
323      * @return Unique result matching specified query
324      * @see #findUniqueResult(String, Object[], org.hibernate.type.Type[], boolean, String)
325      * @see org.springframework.orm.hibernate3.HibernateCallback
326      */

327     public Object JavaDoc findUniqueResult(String JavaDoc hql, Object JavaDoc[] args, boolean cacheable, String JavaDoc cacheRegion) {
328         return findUniqueResult(hql, args, null, cacheable, cacheRegion);
329     }
330
331     /**
332      * Returns unique result matching specified query using <code>org.springframework.orm.hibernate3.HibernateCallback</code>
333      * with arguments and their types
334      *
335      * @param hql Query to execute
336      * @param args Arguments to set
337      * @param types Types of arguments
338      * @return Unique result matching specified query
339      * @see #findUniqueResult(String, Object[], org.hibernate.type.Type[], boolean, String)
340      * @see org.springframework.orm.hibernate3.HibernateCallback
341      */

342     public Object JavaDoc findUniqueResult(String JavaDoc hql, Object JavaDoc[] args, Type[] types) {
343         return findUniqueResult(hql, args, types, false, null);
344     }
345
346     /**
347      * Returns unique result matching specified query using <code>org.springframework.orm.hibernate3.HibernateCallback</code>
348      * with arguments and their types
349      *
350      * @param hql Query to execute
351      * @param args Arguments to set
352      * @param types Types of arguments
353      * @param cacheable <code>true</code> if the query is cacheable
354      * @param cacheRegion region of cache. E.g. one that used in configuration file of EHCahce (ehcache.xml)
355      * @return Unique result matching specified query
356      */

357     public Object JavaDoc findUniqueResult(final String JavaDoc hql, final Object JavaDoc[] args, final Type[] types, final boolean cacheable, final String JavaDoc cacheRegion) {
358         return getHibernateTemplate().execute(new HibernateCallback() {
359             public Object JavaDoc doInHibernate(Session session) throws HibernateException {
360                 Query query = session.createQuery(hql);
361                 query.setCacheable(cacheable);
362                 if ( cacheRegion != null ) {
363                     query.setCacheRegion(cacheRegion);
364                 }
365                 if ( args != null ) {
366                     for ( int i = 0; i < args.length; i++ ) {
367                         Object JavaDoc arg = args[i];
368                         Type type = null;
369                         if ( types != null && i < types.length ) {
370                             type = types[i];
371                         }
372                         if ( type == null ) {
373                             query.setParameter(i, arg);
374                         } else {
375                             query.setParameter(i, arg, type);
376                         }
377                     }
378                 }
379                 return query.uniqueResult();
380             }
381         });
382     }
383
384     /**
385      * <p>Execute some update/delete HQL<p>
386      * <p>CAUTION!!! Be careful it will work only with ast parser (not with org.hibernate.hql.classic.ClassicQueryTranslatorFactory)<p>
387      *
388      * @param hql Query to execute
389      */

390     public void executeUpdate(final String JavaDoc hql) {
391         executeUpdate(hql, null, null);
392     }
393
394     /**
395      * <p>Execute some update/delete HQL</p>
396      * <p>CAUTION!!! Be careful it will work only with ast parser (not with org.hibernate.hql.classic.ClassicQueryTranslatorFactory)</p>
397      *
398      * @param hql Query to execute
399      * @param args Arguments to set
400      */

401     public void executeUpdate(final String JavaDoc hql, final Object JavaDoc[] args) {
402         executeUpdate(hql, args, null);
403     }
404
405     /**
406      * <p>Execute some update/delete HQL</p>
407      * <p>CAUTION!!! Be careful it will work only with ast parser (not with org.hibernate.hql.classic.ClassicQueryTranslatorFactory)</p>
408      *
409      * @param hql Query to execute
410      * @param args Arguments to set
411      * @param types Types of arguments
412      */

413     public void executeUpdate(final String JavaDoc hql, final Object JavaDoc[] args, final Type[] types) {
414         getHibernateTemplate().execute(new HibernateCallback() {
415             public Object JavaDoc doInHibernate(Session session) throws HibernateException {
416                 Query query = session.createQuery(hql);
417                 if ( args != null ) {
418                     for ( int i = 0; i < args.length; i++ ) {
419                         Object JavaDoc arg = args[i];
420                         Type type = null;
421                         if ( types != null && i < types.length ) {
422                             type = types[i];
423                         }
424                         if ( type == null ) {
425                             query.setParameter(i, arg);
426                         } else {
427                             query.setParameter(i, arg, type);
428                         }
429                     }
430                 }
431                 query.executeUpdate();
432                 return null;
433             }
434         });
435
436     }
437
438 }
439
Popular Tags