KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > riot > hibernate > support > HibernateHelper


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2007
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.riot.hibernate.support;
25
26 import java.io.Serializable JavaDoc;
27 import java.util.List JavaDoc;
28
29 import org.hibernate.Criteria;
30 import org.hibernate.HibernateException;
31 import org.hibernate.LockMode;
32 import org.hibernate.Query;
33 import org.hibernate.Session;
34 import org.hibernate.SessionFactory;
35 import org.springframework.dao.DataAccessException;
36 import org.springframework.orm.hibernate3.SessionFactoryUtils;
37
38 /**
39  * Class that provides helper methods to work with contextual sessions.
40  * All HibernateExceptions are caught and converted to DataAccessExceptions.
41  *
42  * @author Felix Gnass [fgnass at neteye dot de]
43  * @since 6.5
44  */

45 public class HibernateHelper extends HibernateSupport {
46
47     private String JavaDoc defaultCacheRegion = null;
48
49     public HibernateHelper(SessionFactory sessionFactory) {
50         this(sessionFactory, null);
51     }
52
53     public HibernateHelper(SessionFactory sessionFactory,
54             String JavaDoc defaultCacheReqion) {
55
56         setSessionFactory(sessionFactory);
57         this.defaultCacheRegion = defaultCacheReqion;
58     }
59
60     /**
61      * Returns the current session.
62      * @see SessionFactory#getCurrentSession()
63      */

64     public Session getSession() {
65         try {
66             return super.getSession();
67         }
68         catch (HibernateException e) {
69             throw SessionFactoryUtils.convertHibernateAccessException(e);
70         }
71     }
72
73     /**
74      * Creates a Criteria for the given Class.
75      */

76     public Criteria createCriteria(Class JavaDoc clazz) {
77         try {
78             return super.createCriteria(clazz);
79         }
80         catch (HibernateException e) {
81             throw SessionFactoryUtils.convertHibernateAccessException(e);
82         }
83     }
84
85     /**
86      * Creates a cacheable Criteria for the given Class, using the helper's
87      * defaultCacheRegion.
88      */

89     public Criteria createCacheableCriteria(Class JavaDoc clazz) {
90         return createCacheableCriteria(defaultCacheRegion, clazz);
91     }
92
93     /**
94      * Creates a cacheable Criteria for the given Class, using the specified
95      * cache region.
96      */

97     public Criteria createCacheableCriteria(String JavaDoc cacheRegion, Class JavaDoc clazz) {
98         try {
99             Criteria c = super.createCriteria(clazz);
100             c.setCacheRegion(cacheRegion);
101             c.setCacheable(true);
102             return c;
103         }
104         catch (HibernateException e) {
105             throw SessionFactoryUtils.convertHibernateAccessException(e);
106         }
107     }
108
109     /**
110      * Creates Query for the given HQL.
111      */

112     public Query createQuery(String JavaDoc hql) {
113         try {
114             return super.createQuery(hql);
115         }
116         catch (HibernateException e) {
117             throw SessionFactoryUtils.convertHibernateAccessException(e);
118         }
119     }
120
121     /**
122      * Creates a cacheable Query for the given HQL, using the helper's
123      * defaultCacheRegion.
124      */

125     public Query createCacheableQuery(String JavaDoc hql) {
126         return createCacheableQuery(defaultCacheRegion, hql);
127     }
128
129     /**
130      * Creates a cacheable Query for the given HQL, using the specified
131      * cache region.
132      */

133     public Query createCacheableQuery(String JavaDoc cacheRegion, String JavaDoc hql) {
134         try {
135             Query query = super.createQuery(hql);
136             query.setCacheRegion(cacheRegion);
137             query.setCacheable(true);
138             return query;
139         }
140         catch (HibernateException e) {
141             throw SessionFactoryUtils.convertHibernateAccessException(e);
142         }
143     }
144
145     /**
146      * Returns the persistent instance of the given Class, assuming that the
147      * instance exists.
148      * @see Session#load(Class, Serializable)
149      */

150     public Object JavaDoc load(Class JavaDoc clazz, Serializable JavaDoc id) throws DataAccessException {
151         try {
152             return getSession().load(clazz, id);
153         }
154         catch (HibernateException e) {
155             throw SessionFactoryUtils.convertHibernateAccessException(e);
156         }
157     }
158
159     /**
160      * Returns the persistent instance of the given Class, or <code>null</code>
161      * if no such instance exists.
162      * @see Session#get(Class, Serializable)
163      */

164     public Object JavaDoc get(Class JavaDoc clazz, Serializable JavaDoc id) throws DataAccessException {
165         try {
166             return getSession().get(clazz, id);
167         }
168         catch (HibernateException e) {
169             throw SessionFactoryUtils.convertHibernateAccessException(e);
170         }
171     }
172
173     /**
174      * Persists the given transient instance.
175      * @see Session#save(Object)
176      */

177     public Serializable JavaDoc save(Object JavaDoc object) throws DataAccessException {
178         try {
179             return getSession().save(object);
180         }
181         catch (HibernateException e) {
182             throw SessionFactoryUtils.convertHibernateAccessException(e);
183         }
184     }
185
186     /**
187      * Updates the given persistent instance.
188      * @see Session#update(Object)
189      */

190     public void update(Object JavaDoc object) throws DataAccessException {
191         try {
192             getSession().update(object);
193         }
194         catch (HibernateException e) {
195             throw SessionFactoryUtils.convertHibernateAccessException(e);
196         }
197     }
198
199     /**
200      * Merges the given detached instance.
201      * @see Session#merge(Object)
202      */

203     public Object JavaDoc merge(Object JavaDoc object) throws DataAccessException {
204         try {
205             return getSession().merge(object);
206         }
207         catch (HibernateException e) {
208             throw SessionFactoryUtils.convertHibernateAccessException(e);
209         }
210     }
211
212     /**
213      * Obtain the specified lock leve upon the given object.
214      * @see Session#lock(Object, LockMode)
215      */

216     public void lock(Object JavaDoc object, LockMode lockMode) throws DataAccessException {
217         try {
218             getSession().lock(object, lockMode);
219         }
220         catch (HibernateException e) {
221             throw SessionFactoryUtils.convertHibernateAccessException(e);
222         }
223     }
224
225     /**
226      * Deletes the given persistent instance.
227      * @see Session#delete(Object)
228      */

229     public void delete(Object JavaDoc object) throws DataAccessException {
230         try {
231             getSession().delete(object);
232         }
233         catch (HibernateException e) {
234             throw SessionFactoryUtils.convertHibernateAccessException(e);
235         }
236     }
237
238     /**
239      * Re-reads the state of the given persistent or detached instance.
240      * @see Session#refresh(Object)
241      */

242     public void refresh(Object JavaDoc object) throws DataAccessException {
243         try {
244             getSession().refresh(object);
245         }
246         catch (HibernateException e) {
247             throw SessionFactoryUtils.convertHibernateAccessException(e);
248         }
249     }
250
251     /**
252      * Forces the current session to flush.
253      * @see Session#flush()
254      */

255     public void flush() throws DataAccessException {
256         try {
257             getSession().flush();
258         }
259         catch (HibernateException e) {
260             throw SessionFactoryUtils.convertHibernateAccessException(e);
261         }
262     }
263
264     public List JavaDoc list(Query query) throws DataAccessException {
265         try {
266             return query.list();
267         }
268         catch (HibernateException e) {
269             throw SessionFactoryUtils.convertHibernateAccessException(e);
270         }
271     }
272
273     public Object JavaDoc uniqueResult(Query query) throws DataAccessException {
274         try {
275             return query.uniqueResult();
276         }
277         catch (HibernateException e) {
278             throw SessionFactoryUtils.convertHibernateAccessException(e);
279         }
280     }
281
282     public List JavaDoc list(Criteria c) throws DataAccessException {
283         try {
284             return c.list();
285         }
286         catch (HibernateException e) {
287             throw SessionFactoryUtils.convertHibernateAccessException(e);
288         }
289     }
290
291     public Object JavaDoc uniqueResult(Criteria c) throws DataAccessException {
292         try {
293             return c.uniqueResult();
294         }
295         catch (HibernateException e) {
296             throw SessionFactoryUtils.convertHibernateAccessException(e);
297         }
298     }
299
300     public void setParameter(Query query, String JavaDoc name, Object JavaDoc val) {
301         if (val != null) {
302             try {
303                 query.setParameter(name, val);
304             }
305             catch (HibernateException e) {
306                 throw SessionFactoryUtils.convertHibernateAccessException(e);
307             }
308         }
309     }
310
311     public int executeUpdate(Query query) {
312         try {
313             return query.executeUpdate();
314         }
315         catch (HibernateException e) {
316             throw SessionFactoryUtils.convertHibernateAccessException(e);
317         }
318     }
319
320 }
321
Popular Tags