1 16 package com.blandware.atleap.persistence.hibernate.core; 17 18 import com.blandware.atleap.common.util.QueryInfo; 19 import com.blandware.atleap.model.core.BaseObject; 20 import com.blandware.atleap.persistence.core.DAO; 21 import org.hibernate.HibernateException; 22 import org.hibernate.Query; 23 import org.hibernate.Session; 24 import org.hibernate.type.Type; 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 import org.springframework.orm.hibernate3.HibernateCallback; 28 import org.springframework.orm.hibernate3.support.HibernateDaoSupport; 29 30 import java.util.List ; 31 32 33 42 public class BaseDAOHibernate extends HibernateDaoSupport implements DAO { 43 44 protected transient final Log log = LogFactory.getLog(getClass()); 45 46 49 public void removeFromCache(BaseObject object) { 50 if (object != null) 51 getHibernateTemplate().evict(object); 52 } 53 54 57 public void reload(Object object) { 58 getHibernateTemplate().refresh(object); 59 } 60 61 70 protected List executeFind(String hql) { 71 return executeFind(hql, null, null, null, false, null); 72 } 73 74 75 86 protected List executeFind(String hql, boolean cacheable, String cacheRegion) { 87 return executeFind(hql, null, null, null, cacheable, cacheRegion); 88 } 89 90 101 protected List executeFind(String hql, Object [] args) { 102 return executeFind(hql, null, args, null, false, null); 103 } 104 105 117 protected List executeFind(String hql, Object [] args, Type[] types) { 118 return executeFind(hql, null, args, types, false, null); 119 } 120 121 133 protected List executeFind(String hql, Object [] args, boolean cacheable, String cacheRegion) { 134 return executeFind(hql, null, args, null, cacheable, cacheRegion); 135 } 136 137 150 protected List executeFind(String hql, Object [] args, Type[] types, boolean cacheable, String cacheRegion) { 151 return executeFind(hql, null, args, types, cacheable, cacheRegion); 152 } 153 154 166 protected List executeFind(String hql, QueryInfo queryInfo) { 167 return executeFind(hql, queryInfo, null, null, false, null); 168 } 169 170 183 protected List executeFind(String hql, QueryInfo queryInfo, Object [] args) { 184 return executeFind(hql, queryInfo, args, null, false, null); 185 } 186 187 201 protected List executeFind(String hql, QueryInfo queryInfo, Object [] args, Type[] types) { 202 return executeFind(hql, queryInfo, args, types, false, null); 203 } 204 205 219 protected List executeFind(String hql, QueryInfo queryInfo, Object [] args, boolean cacheable, String cacheRegion) { 220 return executeFind(hql, queryInfo, args, null, cacheable, cacheRegion); 221 } 222 223 237 protected List executeFind(final String hql, final QueryInfo queryInfo, final Object [] args, final Type[] types, final boolean cacheable, final String cacheRegion) { 238 return getHibernateTemplate().executeFind(new HibernateCallback() { 239 public Object doInHibernate(Session session) throws HibernateException { 240 Query query = session.createQuery(hql); 241 query.setCacheable(cacheable); 242 if ( cacheRegion != null ) { 243 query.setCacheRegion(cacheRegion); 244 } 245 if ( args != null ) { 246 for ( int i = 0; i < args.length; i++ ) { 247 Object arg = args[i]; 248 Type type = null; 249 if ( types != null && i < types.length ) { 250 type = types[i]; 251 } 252 if ( type == null ) { 253 query.setParameter(i, arg); 254 } else { 255 query.setParameter(i, arg, type); 256 } 257 } 258 } 259 if ( queryInfo != null ) { 260 if ( queryInfo.getLimit() != null ) { 261 query.setMaxResults(queryInfo.getLimit().intValue()); 262 } 263 if ( queryInfo.getOffset() != null ) { 264 query.setFirstResult(queryInfo.getOffset().intValue()); 265 } 266 } 267 return query.list(); 268 } 269 }); 270 } 271 272 273 282 public Object findUniqueResult(String hql) { 283 return findUniqueResult(hql, null, null, false, null); 284 } 285 286 297 public Object findUniqueResult(String hql, boolean cacheable, String cacheRegion) { 298 return findUniqueResult(hql, null, null, cacheable, cacheRegion); 299 } 300 301 311 public Object findUniqueResult(String hql, Object [] args) { 312 return findUniqueResult(hql, args, null, false, null); 313 } 314 315 327 public Object findUniqueResult(String hql, Object [] args, boolean cacheable, String cacheRegion) { 328 return findUniqueResult(hql, args, null, cacheable, cacheRegion); 329 } 330 331 342 public Object findUniqueResult(String hql, Object [] args, Type[] types) { 343 return findUniqueResult(hql, args, types, false, null); 344 } 345 346 357 public Object findUniqueResult(final String hql, final Object [] args, final Type[] types, final boolean cacheable, final String cacheRegion) { 358 return getHibernateTemplate().execute(new HibernateCallback() { 359 public Object doInHibernate(Session session) throws HibernateException { 360 Query query = session.createQuery(hql); 361 query.setCacheable(cacheable); 362 if ( cacheRegion != null ) { 363 query.setCacheRegion(cacheRegion); 364 } 365 if ( args != null ) { 366 for ( int i = 0; i < args.length; i++ ) { 367 Object arg = args[i]; 368 Type type = null; 369 if ( types != null && i < types.length ) { 370 type = types[i]; 371 } 372 if ( type == null ) { 373 query.setParameter(i, arg); 374 } else { 375 query.setParameter(i, arg, type); 376 } 377 } 378 } 379 return query.uniqueResult(); 380 } 381 }); 382 } 383 384 390 public void executeUpdate(final String hql) { 391 executeUpdate(hql, null, null); 392 } 393 394 401 public void executeUpdate(final String hql, final Object [] args) { 402 executeUpdate(hql, args, null); 403 } 404 405 413 public void executeUpdate(final String hql, final Object [] args, final Type[] types) { 414 getHibernateTemplate().execute(new HibernateCallback() { 415 public Object doInHibernate(Session session) throws HibernateException { 416 Query query = session.createQuery(hql); 417 if ( args != null ) { 418 for ( int i = 0; i < args.length; i++ ) { 419 Object arg = args[i]; 420 Type type = null; 421 if ( types != null && i < types.length ) { 422 type = types[i]; 423 } 424 if ( type == null ) { 425 query.setParameter(i, arg); 426 } else { 427 query.setParameter(i, arg, type); 428 } 429 } 430 } 431 query.executeUpdate(); 432 return null; 433 } 434 }); 435 436 } 437 438 } 439 | Popular Tags |