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   package com.hack23.cia.service.dao;
6   
7   import java.io.Serializable;
8   import java.util.List;
9   
10  import org.apache.commons.logging.Log;
11  import org.apache.commons.logging.LogFactory;
12  import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
13  import org.springframework.transaction.annotation.Propagation;
14  import org.springframework.transaction.annotation.Transactional;
15  
16  import com.hack23.cia.model.core.impl.BaseEntity;
17  
18  /***
19   * The Class GenericHibernateDAO.
20   *
21   * @param <T> the generic type
22   * @param <ID> the generic type
23   */
24  @Transactional(propagation = Propagation.SUPPORTS)
25  public abstract class GenericHibernateDAO<T extends BaseEntity, ID extends Serializable>
26          extends HibernateDaoSupport implements GenericDAO<T, ID> {
27  
28      /*** The Constant LOGGER. */
29      private static final Log LOGGER = LogFactory
30              .getLog(GenericHibernateDAO.class);
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      protected GenericHibernateDAO(final Class<T> persistentClass) {
41          this.persistentClass = persistentClass;
42      }
43  
44      /*
45       * (non-Javadoc)
46       * 
47       * @see
48       * com.hack23.cia.service.dao.GenericDAO#delete(com.hack23.cia.model.core
49       * .BaseEntity)
50       */
51      @Override
52  	public final void delete(final T entity) {
53          this.getHibernateTemplate().delete(entity);
54          LOGGER.info("Deleted: " + entity); //$NON-NLS-1$
55      }
56  
57      /*
58       * (non-Javadoc)
59       * 
60       * @see com.hack23.cia.service.dao.GenericDAO#getAll()
61       */
62      @Override
63  	@Transactional(readOnly = true)
64      public final List<T> getAll() {
65          return getHibernateTemplate().loadAll(persistentClass);
66      }
67  
68      /*
69       * (non-Javadoc)
70       * 
71       * @see com.hack23.cia.service.dao.GenericDAO#load(java.io.Serializable)
72       */
73      @Override
74  	@Transactional(readOnly = true)
75      public final T load(final ID id) {
76          return this.getHibernateTemplate().get(persistentClass, id);
77      }
78  
79      /*
80       * (non-Javadoc)
81       * 
82       * @see
83       * com.hack23.cia.service.dao.GenericDAO#save(com.hack23.cia.model.core.
84       * BaseEntity)
85       */
86      @Override
87  	public final T save(final T entity) {
88          if (entity.isNew()) {
89              this.getHibernateTemplate().save(entity);
90          } else {
91              this.getHibernateTemplate().saveOrUpdate(entity);
92          }
93          return entity;
94      }
95  }