View Javadoc
1   /*******************************************************************************
2    *   Gisgraphy Project 
3    * 
4    *   This library is free software; you can redistribute it and/or
5    *   modify it under the terms of the GNU Lesser General Public
6    *   License as published by the Free Software Foundation; either
7    *   version 2.1 of the License, or (at your option) any later version.
8    * 
9    *   This library is distributed in the hope that it will be useful,
10   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   *   Lesser General Public License for more details.
13   * 
14   *   You should have received a copy of the GNU Lesser General Public
15   *   License along with this library; if not, write to the Free Software
16   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
17   * 
18   *  Copyright 2008  Gisgraphy project 
19   *  David Masclet <davidmasclet@gisgraphy.com>
20   *  
21   *  
22   *******************************************************************************/
23  package com.gisgraphy.dao.hibernate;
24  
25  import java.io.Serializable;
26  import java.util.List;
27  
28  import org.apache.commons.logging.Log;
29  import org.apache.commons.logging.LogFactory;
30  import org.springframework.orm.ObjectRetrievalFailureException;
31  import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
32  
33  import com.gisgraphy.dao.GenericDao;
34  
35  /**
36   * This class serves as the Base class for all other DAOs - namely to hold
37   * common CRUD methods that they might all use. You should only need to extend
38   * this class when your require custom CRUD logic.
39   * <p>
40   * To register this class in your Spring context file, use the following XML.
41   * 
42   * <pre>
43   *      &lt;bean id=&quot;fooDao&quot; class=&quot;com.gisgraphy.dao.hibernate.GenericDaoHibernate&quot;&gt;
44   *          &lt;constructor-arg value=&quot;com.gisgraphy.model.Foo&quot;/&gt;
45   *          &lt;property name=&quot;sessionFactory&quot; ref=&quot;sessionFactory&quot;/&gt;
46   *      &lt;/bean&gt;
47   * </pre>
48   * 
49   * @author <a href="mailto:bwnoll@gmail.com">Bryan Noll</a>
50   * @param <T>
51   *                a type variable
52   * @param <PK>
53   *                the primary key for that type
54   */
55  public class GenericDaoHibernate<T, PK extends Serializable> extends
56  	HibernateDaoSupport implements GenericDao<T, PK> {
57      /**
58       * Log variable for all child classes. Uses LogFactory.getLog(getClass())
59       * from Commons Logging
60       */
61      protected final Log log = LogFactory.getLog(getClass());
62  
63      private Class<T> persistentClass;
64  
65      /**
66       * Constructor that takes in a class to see which type of entity to persist
67       * 
68       * @param persistentClass
69       *                the class type you'd like to persist
70       */
71      public GenericDaoHibernate(final Class<T> persistentClass) {
72  	this.persistentClass = persistentClass;
73      }
74  
75      /**
76       * {@inheritDoc}
77       */
78      @SuppressWarnings("unchecked")
79      public List<T> getAll() {
80  	return super.getHibernateTemplate().loadAll(this.persistentClass);
81      }
82  
83      /**
84       * {@inheritDoc}
85       */
86      @SuppressWarnings("unchecked")
87      public T get(PK id) {
88  	T entity = (T) super.getHibernateTemplate().get(this.persistentClass,
89  		id);
90  
91  	if (entity == null) {
92  	    log.warn("Uh oh, '" + this.persistentClass + "' object with id '"
93  		    + id + "' not found...");
94  	    throw new ObjectRetrievalFailureException(this.persistentClass, id);
95  	}
96  
97  	return entity;
98      }
99  
100     /**
101      * {@inheritDoc}
102      */
103     @SuppressWarnings("unchecked")
104     public boolean exists(PK id) {
105 	T entity = (T) super.getHibernateTemplate().get(this.persistentClass,
106 		id);
107 	return entity != null;
108     }
109 
110     /**
111      * {@inheritDoc}
112      */
113     @SuppressWarnings("unchecked")
114     public T save(T object) {
115 	return (T) super.getHibernateTemplate().merge(object);
116     }
117 
118     /**
119      * {@inheritDoc}
120      */
121     public void remove(PK id) {
122 	super.getHibernateTemplate().delete(this.get(id));
123     }
124 }