View Javadoc
1   package org.itracker.persistence.dao;
2   
3   import org.hibernate.HibernateException;
4   import org.itracker.model.Entity;
5   import org.springframework.dao.InvalidDataAccessResourceUsageException;
6   import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
7   
8   import java.util.Date;
9   
10  /**
11   * Contains common behaviour to all hibernate factories
12   *
13   * @author rui silva
14   */
15  public abstract class BaseHibernateDAOImpl<T extends Entity> extends HibernateDaoSupport
16          implements BaseDAO<T> {
17  
18      /**
19       *
20       */
21      public BaseHibernateDAOImpl() {
22          super();
23      }
24  
25      /**
26       * insert a new entity.
27       * create- and lastmodified-date is set with current time.
28       *
29       * @param entity - detached entity object
30       */
31      public void save(T entity) {
32          if (null == entity) {
33              throw new InvalidDataAccessResourceUsageException("entity must not be null");
34          }
35          try {
36              Date createDate = new Date();
37              entity.setCreateDate(createDate);
38              entity.setLastModifiedDate(createDate);
39              getSession().save(entity);
40          } catch (HibernateException ex) {
41              logger.debug("save: caught HibernateException, call to convertHibernateException", ex);
42              throw convertHibernateAccessException(ex);
43          }
44      }
45  
46      /**
47       * inserts a new detached entity or updates if it already exists.
48       * create- and update-date are set automatically.
49       *
50       * @param entity - entity object to be inserted or updated
51       */
52      public void saveOrUpdate(T entity) {
53          if (null == entity) {
54              throw new InvalidDataAccessResourceUsageException("entity must not be null");
55          }
56  
57          try {
58              if (null == entity.getCreateDate()) {
59                  entity.setCreateDate(new Date());
60                  entity.setLastModifiedDate(entity.getCreateDate());
61              } else {
62                  entity.setLastModifiedDate(new Date());
63              }
64              getSession().saveOrUpdate(entity);
65          } catch (HibernateException ex) {
66              logger.debug("saveOrUpdate: caught HibernateException, call to convertHibernateException", ex);
67              throw convertHibernateAccessException(ex);
68          }
69      }
70  
71      public void delete(T entity) {
72          if (null == entity) {
73              throw new InvalidDataAccessResourceUsageException("entity must not be null");
74          }
75          try {
76              getSession().delete(entity);
77              getSession().flush();
78          } catch (HibernateException ex) {
79              logger.debug("delete: caught HibernateException, call to convertHibernateException", ex);
80              throw convertHibernateAccessException(ex);
81          }
82      }
83  
84      public void detach(T entity) {
85          if (null == entity) {
86              throw new InvalidDataAccessResourceUsageException("entity must not be null");
87          }
88          try {
89              getSession().evict(entity);
90          } catch (HibernateException ex) {
91              logger.debug("detach: caught HibernateException, call to convertHibernateException", ex);
92              throw convertHibernateAccessException(ex);
93          }
94      }
95  
96      public void refresh(T entity) {
97          if (null == entity) {
98              throw new InvalidDataAccessResourceUsageException("entity must not be null");
99          }
100         try {
101             getSession().refresh(entity);
102         } catch (HibernateException ex) {
103             logger.debug("refresh: caught HibernateException, call to convertHibernateException", ex);
104             throw convertHibernateAccessException(ex);
105         }
106     }
107 
108     @SuppressWarnings("unchecked")
109     public T merge(T entity) {
110         if (null == entity) {
111             throw new InvalidDataAccessResourceUsageException("entity must not be null");
112         }
113         try {
114             return (T) getSession().merge(entity);
115         } catch (HibernateException ex) {
116             logger.debug("merge: caught HibernateException, call to convertHibernateException", ex);
117             throw convertHibernateAccessException(ex);
118         }
119     }
120 }