KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > efs > openreports > providers > HibernateProvider


1 /*
2  * Copyright (C) 2006 Erik Swenson - erik@oreports.com
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License as published by the Free
6  * Software Foundation; either version 2 of the License, or (at your option)
7  * any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16  * Place - Suite 330, Boston, MA 02111-1307, USA.
17  *
18  */

19
20 package org.efs.openreports.providers;
21
22 import java.io.Serializable JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.hibernate.*;
26 import org.hibernate.cfg.Configuration;
27 import org.hibernate.exception.ConstraintViolationException;
28
29 import org.apache.log4j.Logger;
30 import org.efs.openreports.providers.persistence.ConstraintException;
31 import org.efs.openreports.util.LocalStrings;
32 import org.springframework.beans.factory.DisposableBean;
33
34 public class HibernateProvider implements DisposableBean
35 {
36     protected static Logger log = Logger.getLogger(HibernateProvider.class.getName());
37     
38     private static SessionFactory sessionFactory;
39
40     public HibernateProvider() throws ProviderException
41     {
42         try
43         {
44             sessionFactory = new Configuration().configure().buildSessionFactory();
45         }
46         catch(HibernateException he)
47         {
48             throw new ProviderException(he);
49         }
50
51         log.info("SessionFactory Created.");
52     }
53     
54     public static Session openSession() throws ProviderException
55     {
56         try
57         {
58             return sessionFactory.openSession();
59         }
60         catch (HibernateException he)
61         {
62             throw new ProviderException(he);
63         }
64     }
65
66     public static void closeSession(Session session) throws ProviderException
67     {
68         try
69         {
70             if (session != null)
71                 session.close();
72         }
73         catch (HibernateException he)
74         {
75             throw new ProviderException(he);
76         }
77     }
78
79     public static void rollbackTransaction(Transaction tx)
80         throws ProviderException
81     {
82         try
83         {
84             if (tx != null)
85                 tx.rollback();
86         }
87         catch (HibernateException he)
88         {
89             throw new ProviderException(he);
90         }
91     }
92
93     public static Object JavaDoc save(Object JavaDoc object) throws ProviderException
94     {
95         Session session = openSession();
96         Transaction tx = null;
97
98         try
99         {
100             tx = session.beginTransaction();
101             session.save(object);
102             tx.commit();
103         }
104         catch (HibernateException he)
105         {
106             rollbackTransaction(tx);
107             
108             if (he instanceof ConstraintViolationException)
109             {
110                 throw new ProviderException(LocalStrings.getString(LocalStrings.ERROR_UNIQUE_CONSTRAINT));
111             }
112             
113             throw new ProviderException(he);
114         }
115         finally
116         {
117             closeSession(session);
118         }
119
120         return object;
121     }
122
123     public static void update(Object JavaDoc object) throws ProviderException
124     {
125         Session session = openSession();
126         Transaction tx = null;
127
128         try
129         {
130             tx = session.beginTransaction();
131             session.update(object);
132             tx.commit();
133         }
134         catch (HibernateException he)
135         {
136             rollbackTransaction(tx);
137             
138             if (he instanceof ConstraintViolationException)
139             {
140                 throw new ProviderException(LocalStrings.getString(LocalStrings.ERROR_UNIQUE_CONSTRAINT));
141             }
142             
143             throw new ProviderException(he);
144         }
145         finally
146         {
147             closeSession(session);
148         }
149     }
150
151     public static void delete(Object JavaDoc object)
152         throws ProviderException, ConstraintException
153     {
154         Session session = openSession();
155         Transaction tx = null;
156
157         try
158         {
159             tx = session.beginTransaction();
160             session.delete(object);
161             tx.commit();
162         }
163         catch (HibernateException he)
164         {
165             rollbackTransaction(tx);
166             
167             if (he instanceof ConstraintViolationException)
168             {
169                 throw new ConstraintException(he.getMessage());
170             }
171
172             throw new ProviderException(he);
173         }
174         finally
175         {
176             closeSession(session);
177         }
178     }
179
180     public static Object JavaDoc load(Class JavaDoc classType, Serializable JavaDoc id)
181         throws ProviderException
182     {
183         Session session = openSession();
184         
185         try
186         {
187             Object JavaDoc object = session.load(classType, id);
188             return object;
189         }
190         catch (HibernateException he)
191         {
192             throw new ProviderException(he);
193         }
194         finally
195         {
196             closeSession(session);
197         }
198     }
199
200     public static List JavaDoc query(String JavaDoc fromClause) throws ProviderException
201     {
202         Session session = openSession();
203         
204         try
205         {
206             Query query = session.createQuery(fromClause);
207             query.setCacheable(true);
208             
209             List JavaDoc list = query.list();
210
211             return list;
212         }
213         catch (HibernateException he)
214         {
215             throw new ProviderException(he);
216         }
217         finally
218         {
219             closeSession(session);
220         }
221     }
222     
223     public void destroy()
224     {
225         try
226         {
227             sessionFactory.close();
228             
229             log.info("SessionFactory closed.");
230         }
231         catch(HibernateException he)
232         {
233             log.warn(he.toString());
234         }
235     }
236
237     public static void setSessionFactory(SessionFactory sessionFactory)
238     {
239         HibernateProvider.sessionFactory = sessionFactory;
240     }
241 }
Popular Tags