1
2
3
4
5
6
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
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
46
47
48
49 @Transactional(readOnly = true)
50 public T load(ID id) throws DataAccessException {
51 return this.getHibernateTemplate().get(persistentClass, id);
52 }
53
54
55
56
57
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
70
71
72
73 public void delete(T entity) throws DataAccessException {
74 this.getHibernateTemplate().delete(entity);
75 }
76
77
78
79
80
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 }