1
2
3
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
46
47
48
49
50
51 @Override
52 public final void delete(final T entity) {
53 this.getHibernateTemplate().delete(entity);
54 LOGGER.info("Deleted: " + entity);
55 }
56
57
58
59
60
61
62 @Override
63 @Transactional(readOnly = true)
64 public final List<T> getAll() {
65 return getHibernateTemplate().loadAll(persistentClass);
66 }
67
68
69
70
71
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
81
82
83
84
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 }