1 19 20 package org.efs.openreports.providers; 21 22 import java.io.Serializable ; 23 import java.util.List ; 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 save(Object 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 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 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 load(Class classType, Serializable id) 181 throws ProviderException 182 { 183 Session session = openSession(); 184 185 try 186 { 187 Object 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 query(String 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 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
|