View Javadoc

1   package net.ramapuram.thomas.dao.hibernate;
2   
3   import org.apache.commons.logging.Log;
4   import org.apache.commons.logging.LogFactory;
5   import net.ramapuram.thomas.dao.GenericDao;
6   import org.hibernate.SessionFactory;
7   import org.springframework.beans.factory.annotation.Autowired;
8   import org.springframework.beans.factory.annotation.Required;
9   import org.springframework.orm.ObjectRetrievalFailureException;
10  import org.springframework.orm.hibernate3.HibernateTemplate;
11  
12  import java.io.Serializable;
13  import java.util.ArrayList;
14  import java.util.Collection;
15  import java.util.LinkedHashSet;
16  import java.util.List;
17  import java.util.Map;
18  
19  /**
20   * This class serves as the Base class for all other DAOs - namely to hold
21   * common CRUD methods that they might all use. You should only need to extend
22   * this class when your require custom CRUD logic.
23   * <p/>
24   * <p>To register this class in your Spring context file, use the following XML.
25   * <pre>
26   *      &lt;bean id="fooDao" class="net.ramapuram.thomas.dao.hibernate.GenericDaoHibernate"&gt;
27   *          &lt;constructor-arg value="net.ramapuram.thomas.model.Foo"/&gt;
28   *      &lt;/bean&gt;
29   * </pre>
30   *
31   * @author <a href="mailto:bwnoll@gmail.com">Bryan Noll</a>
32   * @param <T> a type variable
33   * @param <PK> the primary key for that type
34   */
35  public class GenericDaoHibernate<T, PK extends Serializable> implements GenericDao<T, PK> {
36      /**
37       * Log variable for all child classes. Uses LogFactory.getLog(getClass()) from Commons Logging
38       */
39      protected final Log log = LogFactory.getLog(getClass());
40      private Class<T> persistentClass;
41      private HibernateTemplate hibernateTemplate;
42      private SessionFactory sessionFactory;
43  
44      /**
45       * Constructor that takes in a class to see which type of entity to persist.
46       * Use this constructor when subclassing.
47       *
48       * @param persistentClass the class type you'd like to persist
49       */
50      public GenericDaoHibernate(final Class<T> persistentClass) {
51          this.persistentClass = persistentClass;
52      }
53  
54      /**
55       * Constructor that takes in a class and sessionFactory for easy creation of DAO.
56       *
57       * @param persistentClass the class type you'd like to persist
58       * @param sessionFactory  the pre-configured Hibernate SessionFactory
59       */
60      public GenericDaoHibernate(final Class<T> persistentClass, SessionFactory sessionFactory) {
61          this.persistentClass = persistentClass;
62          this.sessionFactory = sessionFactory;
63          this.hibernateTemplate = new HibernateTemplate(sessionFactory);
64      }
65  
66      public HibernateTemplate getHibernateTemplate() {
67          return this.hibernateTemplate;
68      }
69  
70      public SessionFactory getSessionFactory() {
71          return this.sessionFactory;
72      }
73  
74      @Autowired
75      @Required
76      public void setSessionFactory(SessionFactory sessionFactory) {
77          this.sessionFactory = sessionFactory;
78          this.hibernateTemplate = new HibernateTemplate(sessionFactory);
79      }
80  
81      /**
82       * {@inheritDoc}
83       */
84      @SuppressWarnings("unchecked")
85      public List<T> getAll() {
86          return hibernateTemplate.loadAll(this.persistentClass);
87      }
88  
89      /**
90       * {@inheritDoc}
91       */
92      @SuppressWarnings("unchecked")
93      public List<T> getAllDistinct() {
94          Collection result = new LinkedHashSet(getAll());
95          return new ArrayList(result);
96      }
97  
98      /**
99       * {@inheritDoc}
100      */
101     @SuppressWarnings("unchecked")
102     public T get(PK id) {
103         T entity = (T) hibernateTemplate.get(this.persistentClass, id);
104 
105         if (entity == null) {
106             log.warn("Uh oh, '" + this.persistentClass + "' object with id '" + id + "' not found...");
107             throw new ObjectRetrievalFailureException(this.persistentClass, id);
108         }
109 
110         return entity;
111     }
112 
113     /**
114      * {@inheritDoc}
115      */
116     @SuppressWarnings("unchecked")
117     public boolean exists(PK id) {
118         T entity = (T) hibernateTemplate.get(this.persistentClass, id);
119         return entity != null;
120     }
121 
122     /**
123      * {@inheritDoc}
124      */
125     @SuppressWarnings("unchecked")
126     public T save(T object) {
127         return (T) hibernateTemplate.merge(object);
128     }
129 
130     /**
131      * {@inheritDoc}
132      */
133     public void remove(PK id) {
134         hibernateTemplate.delete(this.get(id));
135     }
136 
137     /**
138      * {@inheritDoc}
139      */
140     @SuppressWarnings("unchecked")
141     public List<T> findByNamedQuery(String queryName, Map<String, Object> queryParams) {
142         String[] params = new String[queryParams.size()];
143         Object[] values = new Object[queryParams.size()];
144         
145         int index = 0;
146         for (String s : queryParams.keySet()) {
147             params[index] = s;
148             values[index++] = queryParams.get(s);
149         }
150 
151         return hibernateTemplate.findByNamedQueryAndNamedParam(queryName, params, values);
152     }
153 }