1 24 package org.riotfamily.riot.hibernate.support; 25 26 import java.io.Serializable ; 27 import java.util.List ; 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 45 public class HibernateHelper extends HibernateSupport { 46 47 private String defaultCacheRegion = null; 48 49 public HibernateHelper(SessionFactory sessionFactory) { 50 this(sessionFactory, null); 51 } 52 53 public HibernateHelper(SessionFactory sessionFactory, 54 String defaultCacheReqion) { 55 56 setSessionFactory(sessionFactory); 57 this.defaultCacheRegion = defaultCacheReqion; 58 } 59 60 64 public Session getSession() { 65 try { 66 return super.getSession(); 67 } 68 catch (HibernateException e) { 69 throw SessionFactoryUtils.convertHibernateAccessException(e); 70 } 71 } 72 73 76 public Criteria createCriteria(Class clazz) { 77 try { 78 return super.createCriteria(clazz); 79 } 80 catch (HibernateException e) { 81 throw SessionFactoryUtils.convertHibernateAccessException(e); 82 } 83 } 84 85 89 public Criteria createCacheableCriteria(Class clazz) { 90 return createCacheableCriteria(defaultCacheRegion, clazz); 91 } 92 93 97 public Criteria createCacheableCriteria(String cacheRegion, Class 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 112 public Query createQuery(String hql) { 113 try { 114 return super.createQuery(hql); 115 } 116 catch (HibernateException e) { 117 throw SessionFactoryUtils.convertHibernateAccessException(e); 118 } 119 } 120 121 125 public Query createCacheableQuery(String hql) { 126 return createCacheableQuery(defaultCacheRegion, hql); 127 } 128 129 133 public Query createCacheableQuery(String cacheRegion, String 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 150 public Object load(Class clazz, Serializable 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 164 public Object get(Class clazz, Serializable 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 177 public Serializable save(Object object) throws DataAccessException { 178 try { 179 return getSession().save(object); 180 } 181 catch (HibernateException e) { 182 throw SessionFactoryUtils.convertHibernateAccessException(e); 183 } 184 } 185 186 190 public void update(Object object) throws DataAccessException { 191 try { 192 getSession().update(object); 193 } 194 catch (HibernateException e) { 195 throw SessionFactoryUtils.convertHibernateAccessException(e); 196 } 197 } 198 199 203 public Object merge(Object object) throws DataAccessException { 204 try { 205 return getSession().merge(object); 206 } 207 catch (HibernateException e) { 208 throw SessionFactoryUtils.convertHibernateAccessException(e); 209 } 210 } 211 212 216 public void lock(Object 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 229 public void delete(Object object) throws DataAccessException { 230 try { 231 getSession().delete(object); 232 } 233 catch (HibernateException e) { 234 throw SessionFactoryUtils.convertHibernateAccessException(e); 235 } 236 } 237 238 242 public void refresh(Object object) throws DataAccessException { 243 try { 244 getSession().refresh(object); 245 } 246 catch (HibernateException e) { 247 throw SessionFactoryUtils.convertHibernateAccessException(e); 248 } 249 } 250 251 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 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 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 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 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 name, Object 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 |