KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ibatis > dao > client > template > HibernateDaoTemplate


1 /*
2  * Copyright 2004 Clinton Begin
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.ibatis.dao.client.template;
17
18 import com.ibatis.dao.client.DaoException;
19 import com.ibatis.dao.client.DaoManager;
20 import com.ibatis.dao.engine.transaction.hibernate.HibernateDaoTransaction;
21 import net.sf.hibernate.*;
22 import net.sf.hibernate.type.Type;
23
24 import java.io.Serializable JavaDoc;
25 import java.sql.Connection JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29
30 /**
31  * A DaoTemplate for Hibernate implementations that provides a
32  * convenient method to access the Hibernate Session.
33  */

34 public abstract class HibernateDaoTemplate extends DaoTemplate {
35
36   /**
37    * The DaoManager that manages this Dao instance will be passed
38    * in as the parameter to this constructor automatically upon
39    * instantiation.
40    *
41    * @param daoManager
42    */

43   public HibernateDaoTemplate(DaoManager daoManager) {
44     super(daoManager);
45   }
46
47   /**
48    * Gets the Hibernate session associated with the current
49    * DaoTransaction that this Dao is working under.
50    *
51    * @return A Hibernate Session instance.
52    */

53   protected Session getSession() {
54     HibernateDaoTransaction trans = (HibernateDaoTransaction) daoManager.getTransaction(this);
55     return trans.getSession();
56   }
57
58   public void flush() {
59     try {
60       getSession().flush();
61     } catch (HibernateException e) {
62       throw new DaoException("Error occurred in a Hibernate DAO. Cause: " + e, e);
63     }
64   }
65
66   public void setFlushMode(FlushMode flushMode) {
67     getSession().setFlushMode(flushMode);
68   }
69
70   public FlushMode getFlushMode() {
71     return getSession().getFlushMode();
72   }
73
74   public SessionFactory getSessionFactory() {
75     return getSession().getSessionFactory();
76   }
77
78   public Connection JavaDoc connection() {
79     try {
80       return getSession().connection();
81     } catch (HibernateException e) {
82       throw new DaoException("Error occurred in a Hibernate DAO. Cause: " + e, e);
83     }
84   }
85
86   public Connection JavaDoc disconnect() {
87     try {
88       return getSession().disconnect();
89     } catch (HibernateException e) {
90       throw new DaoException("Error occurred in a Hibernate DAO. Cause: " + e, e);
91     }
92   }
93
94   public void reconnect() {
95     try {
96       getSession().reconnect();
97     } catch (HibernateException e) {
98       throw new DaoException("Error occurred in a Hibernate DAO. Cause: " + e, e);
99     }
100   }
101
102   public void reconnect(Connection JavaDoc connection) {
103     try {
104       getSession().reconnect(connection);
105     } catch (HibernateException e) {
106       throw new DaoException(e);
107     }
108   }
109
110   public Connection JavaDoc close() {
111     try {
112       return getSession().close();
113     } catch (HibernateException e) {
114       throw new DaoException("Error occurred in a Hibernate DAO. Cause: " + e, e);
115     }
116   }
117
118   public void cancelQuery() {
119     try {
120       getSession().cancelQuery();
121     } catch (HibernateException e) {
122       throw new DaoException("Error occurred in a Hibernate DAO. Cause: " + e, e);
123     }
124   }
125
126   public boolean isOpen() {
127     return getSession().isOpen();
128   }
129
130   public boolean isConnected() {
131     return getSession().isConnected();
132   }
133
134   public boolean isDirty() {
135     try {
136       return getSession().isDirty();
137     } catch (HibernateException e) {
138       throw new DaoException("Error occurred in a Hibernate DAO. Cause: " + e, e);
139     }
140   }
141
142   public Serializable JavaDoc getIdentifier(Object JavaDoc o) {
143     try {
144       return getSession().getIdentifier(o);
145     } catch (HibernateException e) {
146       throw new DaoException("Error occurred in a Hibernate DAO. Cause: " + e, e);
147     }
148   }
149
150   public boolean contains(Object JavaDoc o) {
151     return getSession().contains(o);
152   }
153
154   public void evict(Object JavaDoc o) {
155     try {
156       getSession().evict(o);
157     } catch (HibernateException e) {
158       throw new DaoException("Error occurred in a Hibernate DAO. Cause: " + e, e);
159     }
160   }
161
162   public Object JavaDoc load(Class JavaDoc aClass, Serializable JavaDoc serializable, LockMode lockMode) {
163     try {
164       return getSession().load(aClass, serializable, lockMode);
165     } catch (HibernateException e) {
166       throw new DaoException("Error occurred in a Hibernate DAO. Cause: " + e, e);
167     }
168   }
169
170   public Object JavaDoc load(Class JavaDoc aClass, Serializable JavaDoc serializable) {
171     try {
172       return getSession().load(aClass, serializable);
173     } catch (HibernateException e) {
174       throw new DaoException("Error occurred in a Hibernate DAO. Cause: " + e, e);
175     }
176   }
177
178   public void load(Object JavaDoc o, Serializable JavaDoc serializable) {
179     try {
180       getSession().load(o, serializable);
181     } catch (HibernateException e) {
182       throw new DaoException("Error occurred in a Hibernate DAO. Cause: " + e, e);
183     }
184   }
185
186   public void replicate(Object JavaDoc o, ReplicationMode replicationMode) {
187     try {
188       getSession().replicate(o, replicationMode);
189     } catch (HibernateException e) {
190       throw new DaoException("Error occurred in a Hibernate DAO. Cause: " + e, e);
191     }
192   }
193
194   public Serializable JavaDoc save(Object JavaDoc o) {
195     try {
196       return getSession().save(o);
197     } catch (HibernateException e) {
198       throw new DaoException("Error occurred in a Hibernate DAO. Cause: " + e, e);
199     }
200   }
201
202   public void save(Object JavaDoc o, Serializable JavaDoc serializable) {
203     try {
204       getSession().save(o, serializable);
205     } catch (HibernateException e) {
206       throw new DaoException("Error occurred in a Hibernate DAO. Cause: " + e, e);
207     }
208   }
209
210   public void saveOrUpdate(Object JavaDoc o) {
211     try {
212       getSession().save(o);
213     } catch (HibernateException e) {
214       throw new DaoException("Error occurred in a Hibernate DAO. Cause: " + e, e);
215     }
216   }
217
218   public void update(Object JavaDoc o) {
219     try {
220       getSession().update(o);
221     } catch (HibernateException e) {
222       throw new DaoException("Error occurred in a Hibernate DAO. Cause: " + e, e);
223     }
224   }
225
226   public void update(Object JavaDoc o, Serializable JavaDoc serializable) {
227     try {
228       getSession().update(o, serializable);
229     } catch (HibernateException e) {
230       throw new DaoException("Error occurred in a Hibernate DAO. Cause: " + e, e);
231     }
232   }
233
234   public Object JavaDoc saveOrUpdateCopy(Object JavaDoc o) {
235     try {
236       return getSession().saveOrUpdateCopy(o);
237     } catch (HibernateException e) {
238       throw new DaoException("Error occurred in a Hibernate DAO. Cause: " + e, e);
239     }
240   }
241
242   public Object JavaDoc saveOrUpdateCopy(Object JavaDoc o, Serializable JavaDoc serializable) {
243     try {
244       return getSession().saveOrUpdateCopy(o, serializable);
245     } catch (HibernateException e) {
246       throw new DaoException("Error occurred in a Hibernate DAO. Cause: " + e, e);
247     }
248   }
249
250   public void delete(Object JavaDoc o) {
251     try {
252       getSession().delete(o);
253     } catch (HibernateException e) {
254       throw new DaoException("Error occurred in a Hibernate DAO. Cause: " + e, e);
255     }
256   }
257
258   public List JavaDoc find(String JavaDoc s) {
259     try {
260       return getSession().find(s);
261     } catch (HibernateException e) {
262       throw new DaoException("Error occurred in a Hibernate DAO. Cause: " + e, e);
263     }
264   }
265
266   public List JavaDoc find(String JavaDoc s, Object JavaDoc o, Type type) {
267     try {
268       return getSession().find(s, o, type);
269     } catch (HibernateException e) {
270       throw new DaoException("Error occurred in a Hibernate DAO. Cause: " + e, e);
271     }
272   }
273
274   public List JavaDoc find(String JavaDoc s, Object JavaDoc[] objects, Type[] types) {
275     try {
276       return getSession().find(s, objects, types);
277     } catch (HibernateException e) {
278       throw new DaoException("Error occurred in a Hibernate DAO. Cause: " + e, e);
279     }
280   }
281
282   public Iterator JavaDoc iterate(String JavaDoc s) {
283     try {
284       return getSession().iterate(s);
285     } catch (HibernateException e) {
286       throw new DaoException("Error occurred in a Hibernate DAO. Cause: " + e, e);
287     }
288   }
289
290   public Iterator JavaDoc iterate(String JavaDoc s, Object JavaDoc o, Type type) {
291     try {
292       return getSession().iterate(s, o, type);
293     } catch (HibernateException e) {
294       throw new DaoException("Error occurred in a Hibernate DAO. Cause: " + e, e);
295     }
296   }
297
298   public Iterator JavaDoc iterate(String JavaDoc s, Object JavaDoc[] objects, Type[] types) {
299     try {
300       return getSession().iterate(s, objects, types);
301     } catch (HibernateException e) {
302       throw new DaoException("Error occurred in a Hibernate DAO. Cause: " + e, e);
303     }
304   }
305
306   public Collection JavaDoc filter(Object JavaDoc o, String JavaDoc s) {
307     try {
308       return getSession().filter(o, s);
309     } catch (HibernateException e) {
310       throw new DaoException("Error occurred in a Hibernate DAO. Cause: " + e, e);
311     }
312   }
313
314   public Collection JavaDoc filter(Object JavaDoc o, String JavaDoc s, Object JavaDoc o1, Type type) {
315     try {
316       return getSession().filter(o, s, o1, type);
317     } catch (HibernateException e) {
318       throw new DaoException("Error occurred in a Hibernate DAO. Cause: " + e, e);
319     }
320   }
321
322   public Collection JavaDoc filter(Object JavaDoc o, String JavaDoc s, Object JavaDoc[] objects, Type[] types) {
323     try {
324       return getSession().filter(o, s, objects, types);
325     } catch (HibernateException e) {
326       throw new DaoException("Error occurred in a Hibernate DAO. Cause: " + e, e);
327     }
328   }
329
330   public int delete(String JavaDoc s) {
331     try {
332       return getSession().delete(s);
333     } catch (HibernateException e) {
334       throw new DaoException("Error occurred in a Hibernate DAO. Cause: " + e, e);
335     }
336   }
337
338   public int delete(String JavaDoc s, Object JavaDoc o, Type type) {
339     try {
340       return getSession().delete(s, o, type);
341     } catch (HibernateException e) {
342       throw new DaoException("Error occurred in a Hibernate DAO. Cause: " + e, e);
343     }
344   }
345
346   public int delete(String JavaDoc s, Object JavaDoc[] objects, Type[] types) {
347     try {
348       return getSession().delete(s, objects, types);
349     } catch (HibernateException e) {
350       throw new DaoException("Error occurred in a Hibernate DAO. Cause: " + e, e);
351     }
352   }
353
354   public void lock(Object JavaDoc o, LockMode lockMode) {
355     try {
356       getSession().lock(o, lockMode);
357     } catch (HibernateException e) {
358       throw new DaoException("Error occurred in a Hibernate DAO. Cause: " + e, e);
359     }
360   }
361
362   public void refresh(Object JavaDoc o) {
363     try {
364       getSession().refresh(o);
365     } catch (HibernateException e) {
366       throw new DaoException("Error occurred in a Hibernate DAO. Cause: " + e, e);
367     }
368   }
369
370   public void refresh(Object JavaDoc o, LockMode lockMode) {
371     try {
372       getSession().refresh(o, lockMode);
373     } catch (HibernateException e) {
374       throw new DaoException("Error occurred in a Hibernate DAO. Cause: " + e, e);
375     }
376   }
377
378   public LockMode getCurrentLockMode(Object JavaDoc o) {
379     try {
380       return getSession().getCurrentLockMode(o);
381     } catch (HibernateException e) {
382       throw new DaoException("Error occurred in a Hibernate DAO. Cause: " + e, e);
383     }
384   }
385
386   public Transaction beginTransaction() {
387     try {
388       return getSession().beginTransaction();
389     } catch (HibernateException e) {
390       throw new DaoException("Error occurred in a Hibernate DAO. Cause: " + e, e);
391     }
392   }
393
394   public Criteria createCriteria(Class JavaDoc aClass) {
395     return getSession().createCriteria(aClass);
396   }
397
398   public Query createQuery(String JavaDoc s) {
399     try {
400       return getSession().createQuery(s);
401     } catch (HibernateException e) {
402       throw new DaoException("Error occurred in a Hibernate DAO. Cause: " + e, e);
403     }
404   }
405
406   public Query createFilter(Object JavaDoc o, String JavaDoc s) {
407     try {
408       return getSession().createFilter(o, s);
409     } catch (HibernateException e) {
410       throw new DaoException("Error occurred in a Hibernate DAO. Cause: " + e, e);
411     }
412   }
413
414   public Query getNamedQuery(String JavaDoc s) {
415     try {
416       return getSession().getNamedQuery(s);
417     } catch (HibernateException e) {
418       throw new DaoException("Error occurred in a Hibernate DAO. Cause: " + e, e);
419     }
420   }
421
422   public Query createSQLQuery(String JavaDoc s, String JavaDoc s1, Class JavaDoc aClass) {
423     return getSession().createSQLQuery(s, s1, aClass);
424   }
425
426   public Query createSQLQuery(String JavaDoc s, String JavaDoc[] strings, Class JavaDoc[] classes) {
427     return getSession().createSQLQuery(s, strings, classes);
428   }
429
430   public void clear() {
431     getSession().clear();
432   }
433
434   public Object JavaDoc get(Class JavaDoc aClass, Serializable JavaDoc serializable) {
435     try {
436       return getSession().get(aClass, serializable);
437     } catch (HibernateException e) {
438       throw new DaoException("Error occurred in a Hibernate DAO. Cause: " + e, e);
439     }
440   }
441
442   public Object JavaDoc get(Class JavaDoc aClass, Serializable JavaDoc serializable, LockMode lockMode) {
443     try {
444       return getSession().get(aClass, serializable, lockMode);
445     } catch (HibernateException e) {
446       throw new DaoException("Error occurred in a Hibernate DAO. Cause: " + e, e);
447     }
448   }
449
450 }
451
Popular Tags