View Javadoc

1   /*
2   Copyright 2010 James Pether Sörling Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. 
3   	$Id
4   */
5   /*
6    @author Pether Sorling
7    */
8   package org.directdemocracyportal.democracy.service.dao.hibernate;
9   
10  import java.io.Serializable;
11  import java.util.List;
12  
13  import org.directdemocracyportal.democracy.model.core.BaseEntity;
14  import org.directdemocracyportal.democracy.service.dao.GenericDAO;
15  import org.springframework.dao.DataAccessException;
16  import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
17  import org.springframework.transaction.annotation.Propagation;
18  import org.springframework.transaction.annotation.Transactional;
19  
20  // TODO: Auto-generated Javadoc
21  /***
22   * The Class GenericHibernateDAO.
23   *
24   * @param <T> the generic type
25   * @param <ID> the generic type
26   */
27  @Transactional(propagation = Propagation.MANDATORY)
28  public abstract class GenericHibernateDAO<T extends BaseEntity, ID extends Serializable>
29          extends HibernateDaoSupport implements GenericDAO<T, ID>
30  {
31  
32      /*** The persistent class. */
33      private final Class<T> persistentClass;
34  
35      /***
36       * Instantiates a new generic hibernate dao.
37       *
38       * @param persistentClass the persistent class
39       */
40      public GenericHibernateDAO(Class<T> persistentClass) {
41          this.persistentClass = persistentClass;
42      }
43  
44      /*
45       * (non-Javadoc)
46       *
47       * @see org.directdemocracyportal.democracy.service.dao.GenericDAO#load(java.io.Serializable)
48       */
49      @Transactional(readOnly = true)
50      public T load(ID id) throws DataAccessException {
51          return this.getHibernateTemplate().get(persistentClass, id);
52      }
53  
54      /*
55       * (non-Javadoc)
56       *
57       * @see org.directdemocracyportal.democracy.service.dao.GenericDAO#save(org.directdemocracyportal.democracy.model.core.BaseEntity)
58       */
59      public T save(T entity) throws DataAccessException {
60          if (entity.isNew()) {
61              this.getHibernateTemplate().save(entity);
62          } else {
63              this.getHibernateTemplate().saveOrUpdate(entity);
64          }
65          return entity;
66      }
67  
68      /*
69       * (non-Javadoc)
70       *
71       * @see org.directdemocracyportal.democracy.service.dao.GenericDAO#delete(org.directdemocracyportal.democracy.model.core.BaseEntity)
72       */
73      public void delete(T entity) throws DataAccessException {
74          this.getHibernateTemplate().delete(entity);
75      }
76  
77      /*
78       * (non-Javadoc)
79       *
80       * @see org.directdemocracyportal.democracy.service.dao.GenericDAO#getAll()
81       */
82      @SuppressWarnings("unchecked")
83      @Transactional(readOnly = true)
84      public List<T> getAll() throws DataAccessException {
85          return getHibernateTemplate().find(
86                  "from " + this.persistentClass.getSimpleName());
87      }
88  }