1 package net.ramapuram.thomas.dao.hibernate;
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5 import net.ramapuram.thomas.dao.GenericDao;
6 import org.hibernate.SessionFactory;
7 import org.springframework.beans.factory.annotation.Autowired;
8 import org.springframework.beans.factory.annotation.Required;
9 import org.springframework.orm.ObjectRetrievalFailureException;
10 import org.springframework.orm.hibernate3.HibernateTemplate;
11
12 import java.io.Serializable;
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.LinkedHashSet;
16 import java.util.List;
17 import java.util.Map;
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 public class GenericDaoHibernate<T, PK extends Serializable> implements GenericDao<T, PK> {
36
37
38
39 protected final Log log = LogFactory.getLog(getClass());
40 private Class<T> persistentClass;
41 private HibernateTemplate hibernateTemplate;
42 private SessionFactory sessionFactory;
43
44
45
46
47
48
49
50 public GenericDaoHibernate(final Class<T> persistentClass) {
51 this.persistentClass = persistentClass;
52 }
53
54
55
56
57
58
59
60 public GenericDaoHibernate(final Class<T> persistentClass, SessionFactory sessionFactory) {
61 this.persistentClass = persistentClass;
62 this.sessionFactory = sessionFactory;
63 this.hibernateTemplate = new HibernateTemplate(sessionFactory);
64 }
65
66 public HibernateTemplate getHibernateTemplate() {
67 return this.hibernateTemplate;
68 }
69
70 public SessionFactory getSessionFactory() {
71 return this.sessionFactory;
72 }
73
74 @Autowired
75 @Required
76 public void setSessionFactory(SessionFactory sessionFactory) {
77 this.sessionFactory = sessionFactory;
78 this.hibernateTemplate = new HibernateTemplate(sessionFactory);
79 }
80
81
82
83
84 @SuppressWarnings("unchecked")
85 public List<T> getAll() {
86 return hibernateTemplate.loadAll(this.persistentClass);
87 }
88
89
90
91
92 @SuppressWarnings("unchecked")
93 public List<T> getAllDistinct() {
94 Collection result = new LinkedHashSet(getAll());
95 return new ArrayList(result);
96 }
97
98
99
100
101 @SuppressWarnings("unchecked")
102 public T get(PK id) {
103 T entity = (T) hibernateTemplate.get(this.persistentClass, id);
104
105 if (entity == null) {
106 log.warn("Uh oh, '" + this.persistentClass + "' object with id '" + id + "' not found...");
107 throw new ObjectRetrievalFailureException(this.persistentClass, id);
108 }
109
110 return entity;
111 }
112
113
114
115
116 @SuppressWarnings("unchecked")
117 public boolean exists(PK id) {
118 T entity = (T) hibernateTemplate.get(this.persistentClass, id);
119 return entity != null;
120 }
121
122
123
124
125 @SuppressWarnings("unchecked")
126 public T save(T object) {
127 return (T) hibernateTemplate.merge(object);
128 }
129
130
131
132
133 public void remove(PK id) {
134 hibernateTemplate.delete(this.get(id));
135 }
136
137
138
139
140 @SuppressWarnings("unchecked")
141 public List<T> findByNamedQuery(String queryName, Map<String, Object> queryParams) {
142 String[] params = new String[queryParams.size()];
143 Object[] values = new Object[queryParams.size()];
144
145 int index = 0;
146 for (String s : queryParams.keySet()) {
147 params[index] = s;
148 values[index++] = queryParams.get(s);
149 }
150
151 return hibernateTemplate.findByNamedQueryAndNamedParam(queryName, params, values);
152 }
153 }