BaseHibernateDAOImpl.java

  1. package org.itracker.persistence.dao;

  2. import org.hibernate.HibernateException;
  3. import org.itracker.model.Entity;
  4. import org.springframework.dao.InvalidDataAccessResourceUsageException;
  5. import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

  6. import java.util.Date;

  7. /**
  8.  * Contains common behaviour to all hibernate factories
  9.  *
  10.  * @author rui silva
  11.  */
  12. public abstract class BaseHibernateDAOImpl<T extends Entity> extends HibernateDaoSupport
  13.         implements BaseDAO<T> {

  14.     /**
  15.      *
  16.      */
  17.     public BaseHibernateDAOImpl() {
  18.         super();
  19.     }

  20.     /**
  21.      * insert a new entity.
  22.      * create- and lastmodified-date is set with current time.
  23.      *
  24.      * @param entity - detached entity object
  25.      */
  26.     public void save(T entity) {
  27.         if (null == entity) {
  28.             throw new InvalidDataAccessResourceUsageException("entity must not be null");
  29.         }
  30.         try {
  31.             Date createDate = new Date();
  32.             entity.setCreateDate(createDate);
  33.             entity.setLastModifiedDate(createDate);
  34.             getSession().save(entity);
  35.         } catch (HibernateException ex) {
  36.             logger.debug("save: caught HibernateException, call to convertHibernateException", ex);
  37.             throw convertHibernateAccessException(ex);
  38.         }
  39.     }

  40.     /**
  41.      * inserts a new detached entity or updates if it already exists.
  42.      * create- and update-date are set automatically.
  43.      *
  44.      * @param entity - entity object to be inserted or updated
  45.      */
  46.     public void saveOrUpdate(T entity) {
  47.         if (null == entity) {
  48.             throw new InvalidDataAccessResourceUsageException("entity must not be null");
  49.         }

  50.         try {
  51.             if (null == entity.getCreateDate()) {
  52.                 entity.setCreateDate(new Date());
  53.                 entity.setLastModifiedDate(entity.getCreateDate());
  54.             } else {
  55.                 entity.setLastModifiedDate(new Date());
  56.             }
  57.             getSession().saveOrUpdate(entity);
  58.         } catch (HibernateException ex) {
  59.             logger.debug("saveOrUpdate: caught HibernateException, call to convertHibernateException", ex);
  60.             throw convertHibernateAccessException(ex);
  61.         }
  62.     }

  63.     public void delete(T entity) {
  64.         if (null == entity) {
  65.             throw new InvalidDataAccessResourceUsageException("entity must not be null");
  66.         }
  67.         try {
  68.             getSession().delete(entity);
  69.             getSession().flush();
  70.         } catch (HibernateException ex) {
  71.             logger.debug("delete: caught HibernateException, call to convertHibernateException", ex);
  72.             throw convertHibernateAccessException(ex);
  73.         }
  74.     }

  75.     public void detach(T entity) {
  76.         if (null == entity) {
  77.             throw new InvalidDataAccessResourceUsageException("entity must not be null");
  78.         }
  79.         try {
  80.             getSession().evict(entity);
  81.         } catch (HibernateException ex) {
  82.             logger.debug("detach: caught HibernateException, call to convertHibernateException", ex);
  83.             throw convertHibernateAccessException(ex);
  84.         }
  85.     }

  86.     public void refresh(T entity) {
  87.         if (null == entity) {
  88.             throw new InvalidDataAccessResourceUsageException("entity must not be null");
  89.         }
  90.         try {
  91.             getSession().refresh(entity);
  92.         } catch (HibernateException ex) {
  93.             logger.debug("refresh: caught HibernateException, call to convertHibernateException", ex);
  94.             throw convertHibernateAccessException(ex);
  95.         }
  96.     }

  97.     @SuppressWarnings("unchecked")
  98.     public T merge(T entity) {
  99.         if (null == entity) {
  100.             throw new InvalidDataAccessResourceUsageException("entity must not be null");
  101.         }
  102.         try {
  103.             return (T) getSession().merge(entity);
  104.         } catch (HibernateException ex) {
  105.             logger.debug("merge: caught HibernateException, call to convertHibernateException", ex);
  106.             throw convertHibernateAccessException(ex);
  107.         }
  108.     }
  109. }