1 | package com.ether.dao; |
---|
2 | |
---|
3 | import static com.ether.PersistenceHelper.convert; |
---|
4 | import static com.ether.PersistenceHelper.uniqueResult; |
---|
5 | |
---|
6 | import java.io.Serializable; |
---|
7 | import java.lang.reflect.ParameterizedType; |
---|
8 | import java.util.ArrayList; |
---|
9 | import java.util.Collection; |
---|
10 | import java.util.List; |
---|
11 | |
---|
12 | import org.hibernate.Criteria; |
---|
13 | import org.hibernate.Session; |
---|
14 | import org.hibernate.SessionFactory; |
---|
15 | import org.hibernate.criterion.DetachedCriteria; |
---|
16 | import org.hibernate.criterion.Example; |
---|
17 | import org.hibernate.criterion.Projections; |
---|
18 | import org.jetbrains.annotations.NotNull; |
---|
19 | import org.jetbrains.annotations.Nullable; |
---|
20 | import org.springframework.dao.DataAccessException; |
---|
21 | |
---|
22 | import com.ether.PersistenceException; |
---|
23 | |
---|
24 | public class DomainAccessObjectImpl<DO, PK extends Serializable> |
---|
25 | implements DomainAccessObject<DO, PK> |
---|
26 | { |
---|
27 | public DomainAccessObjectImpl(final Class<DO> domainObjectClass) { |
---|
28 | _domainObjectClass = domainObjectClass; |
---|
29 | } |
---|
30 | |
---|
31 | // ************************************************************************** // |
---|
32 | // ******************************* REQUESTS ********************************* // |
---|
33 | // ************************************************************************** // |
---|
34 | |
---|
35 | // ******************************* SELECT BY CRITERIA **// |
---|
36 | @Nullable |
---|
37 | public final DO selectByCriteria(final DetachedCriteria criteria) |
---|
38 | throws PersistenceException |
---|
39 | { |
---|
40 | return selectByCriteria(getDomainObjectClass(), criteria); |
---|
41 | } |
---|
42 | |
---|
43 | @Nullable |
---|
44 | public final <O> O selectByCriteria(final Class<O> objectClass, final DetachedCriteria criteria) throws PersistenceException |
---|
45 | { |
---|
46 | return (O) criteria.getExecutableCriteria(_sessionFactory.getCurrentSession()).uniqueResult(); |
---|
47 | } |
---|
48 | |
---|
49 | @NotNull |
---|
50 | public final <O> List<O> selectAllByCriteria(final Class<O> objectClass, final DetachedCriteria criteria) throws PersistenceException |
---|
51 | { |
---|
52 | final List<O> result = (List<O>) criteria.getExecutableCriteria(_sessionFactory.getCurrentSession()).list(); |
---|
53 | return null != result ? result : new ArrayList<O>(); |
---|
54 | } |
---|
55 | |
---|
56 | @NotNull |
---|
57 | public final List<DO> selectAllByCriteria(final DetachedCriteria criteria) |
---|
58 | throws PersistenceException |
---|
59 | { |
---|
60 | return selectAllByCriteria(getDomainObjectClass(), criteria); |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | // ******************************* INSERT **// |
---|
65 | @NotNull |
---|
66 | public final PK insert(final DO domainObject) throws PersistenceException |
---|
67 | { |
---|
68 | try |
---|
69 | { |
---|
70 | final Session session = _sessionFactory.getCurrentSession(); |
---|
71 | final PK primaryKey = (PK) session.save(domainObject); |
---|
72 | session.flush(); |
---|
73 | return primaryKey; |
---|
74 | } |
---|
75 | catch (DataAccessException e) |
---|
76 | { |
---|
77 | throw convert(e); |
---|
78 | } |
---|
79 | } |
---|
80 | |
---|
81 | public final void insertAll(@NotNull final Collection<DO> domainObjects) |
---|
82 | throws PersistenceException |
---|
83 | { |
---|
84 | try |
---|
85 | { |
---|
86 | final Session session = _sessionFactory.getCurrentSession(); |
---|
87 | int i = 0; |
---|
88 | for (final DO domainObject : domainObjects) |
---|
89 | { |
---|
90 | session.save(domainObject); |
---|
91 | // Avoid OutOfMemoryError |
---|
92 | if (i >= 500) |
---|
93 | { |
---|
94 | session.flush(); |
---|
95 | session.clear(); |
---|
96 | i = 0; |
---|
97 | } |
---|
98 | } |
---|
99 | session.flush(); |
---|
100 | } |
---|
101 | catch (DataAccessException e) |
---|
102 | { |
---|
103 | throw convert(e); |
---|
104 | } |
---|
105 | } |
---|
106 | |
---|
107 | // ******************************* UPDATE **// |
---|
108 | public void update(@NotNull final DO domainObject) |
---|
109 | throws PersistenceException |
---|
110 | { |
---|
111 | try |
---|
112 | { |
---|
113 | final Session session = _sessionFactory.getCurrentSession(); |
---|
114 | session.update(domainObject); |
---|
115 | session.flush(); |
---|
116 | } |
---|
117 | catch (DataAccessException e) |
---|
118 | { |
---|
119 | throw convert(e); |
---|
120 | } |
---|
121 | } |
---|
122 | |
---|
123 | public final void updateAll(final Collection<? extends DO> domainObjects) |
---|
124 | throws PersistenceException |
---|
125 | { |
---|
126 | try |
---|
127 | { |
---|
128 | final Session session = _sessionFactory.getCurrentSession(); |
---|
129 | for (final DO domainObject : domainObjects) |
---|
130 | session.saveOrUpdate(domainObject); |
---|
131 | session.flush(); |
---|
132 | } |
---|
133 | catch (DataAccessException e) |
---|
134 | { |
---|
135 | throw convert(e); |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | // ******************************* UPGRADE **// |
---|
140 | public final void upgrade(final DO domainObject) |
---|
141 | throws PersistenceException |
---|
142 | { |
---|
143 | try |
---|
144 | { |
---|
145 | final Session session = _sessionFactory.getCurrentSession(); |
---|
146 | session.refresh(domainObject); |
---|
147 | session.flush(); |
---|
148 | } |
---|
149 | catch (DataAccessException e) |
---|
150 | { |
---|
151 | throw convert(e); |
---|
152 | } |
---|
153 | } |
---|
154 | |
---|
155 | public final void upgradeAll(final Collection<DO> domainObjects) |
---|
156 | throws PersistenceException |
---|
157 | { |
---|
158 | try |
---|
159 | { |
---|
160 | final Session session = _sessionFactory.getCurrentSession(); |
---|
161 | for (final DO domainObject : domainObjects) |
---|
162 | { |
---|
163 | session.refresh(domainObject); |
---|
164 | } |
---|
165 | session.flush(); |
---|
166 | } |
---|
167 | catch (DataAccessException e) |
---|
168 | { |
---|
169 | throw convert(e); |
---|
170 | } |
---|
171 | } |
---|
172 | |
---|
173 | // ******************************* DELETE **// |
---|
174 | public void deleteByPrimaryKey(final PK primaryKey) |
---|
175 | throws PersistenceException |
---|
176 | { |
---|
177 | try |
---|
178 | { |
---|
179 | final Session session = _sessionFactory.getCurrentSession(); |
---|
180 | final Object domainObject = session.get(getDomainObjectClass(), primaryKey); |
---|
181 | if(null != domainObject) |
---|
182 | session.delete(domainObject); |
---|
183 | else |
---|
184 | throw new PersistenceException(PersistenceException.PersistenceCode.OBJECT_TO_DELETE_NOT_FOUND); |
---|
185 | session.flush(); |
---|
186 | } |
---|
187 | catch (DataAccessException e) |
---|
188 | { |
---|
189 | throw convert(e); |
---|
190 | } |
---|
191 | } |
---|
192 | |
---|
193 | public final void delete(@NotNull final DO domainObject) |
---|
194 | throws PersistenceException |
---|
195 | { |
---|
196 | try |
---|
197 | { |
---|
198 | final Session session = _sessionFactory.getCurrentSession(); |
---|
199 | session.delete(domainObject); |
---|
200 | session.flush(); |
---|
201 | } |
---|
202 | catch (DataAccessException e) |
---|
203 | { |
---|
204 | throw convert(e); |
---|
205 | } |
---|
206 | } |
---|
207 | |
---|
208 | public final void deleteAll(final Collection<DO> domainObjects) |
---|
209 | throws PersistenceException |
---|
210 | { |
---|
211 | try |
---|
212 | { |
---|
213 | final Session session = _sessionFactory.getCurrentSession(); |
---|
214 | for (final DO domainObject : domainObjects) |
---|
215 | session.delete(domainObject); |
---|
216 | session.flush(); |
---|
217 | } |
---|
218 | catch (DataAccessException e) |
---|
219 | { |
---|
220 | throw convert(e); |
---|
221 | } |
---|
222 | } |
---|
223 | |
---|
224 | // ******************************* SELECT **// |
---|
225 | public final DO selectByPrimaryKey(@NotNull final PK primaryKey) |
---|
226 | throws PersistenceException |
---|
227 | { |
---|
228 | try |
---|
229 | { |
---|
230 | final Session session = _sessionFactory.getCurrentSession(); |
---|
231 | return (DO) session.get(getDomainObjectClass(), primaryKey); |
---|
232 | } |
---|
233 | catch (DataAccessException e) |
---|
234 | { |
---|
235 | throw convert(e); |
---|
236 | } |
---|
237 | } |
---|
238 | |
---|
239 | public final DO selectByExample(final DO domainObject) |
---|
240 | throws PersistenceException |
---|
241 | { |
---|
242 | final Session session = _sessionFactory.getCurrentSession(); |
---|
243 | final Criteria executableCriteria = session.createCriteria(domainObject.getClass()); |
---|
244 | executableCriteria.add(Example.create(domainObject)); |
---|
245 | return uniqueResult((Collection<DO>) executableCriteria.list()); |
---|
246 | } |
---|
247 | |
---|
248 | public final List<DO> selectAllByExample(final DO domainObject) |
---|
249 | throws PersistenceException |
---|
250 | { |
---|
251 | final Session session = _sessionFactory.getCurrentSession(); |
---|
252 | final Criteria executableCriteria = session.createCriteria(domainObject.getClass()); |
---|
253 | executableCriteria.add(Example.create(domainObject)); |
---|
254 | return (List<DO>) executableCriteria.list(); |
---|
255 | } |
---|
256 | |
---|
257 | public final List<DO> selectAll() throws PersistenceException |
---|
258 | { |
---|
259 | try |
---|
260 | { |
---|
261 | final Session session = _sessionFactory.getCurrentSession(); |
---|
262 | final Criteria executableCriteria = session.createCriteria(getDomainObjectClass()); |
---|
263 | return (List<DO>) executableCriteria.list(); |
---|
264 | } |
---|
265 | catch (DataAccessException e) |
---|
266 | { |
---|
267 | throw convert(e); |
---|
268 | } |
---|
269 | } |
---|
270 | |
---|
271 | // ******************************* PAGINATE **// |
---|
272 | protected <T> List<T> selectPage(@Nullable final Integer firstResult, @Nullable final Integer maxResults, final DetachedCriteria detachedCriteria) |
---|
273 | throws PersistenceException |
---|
274 | { |
---|
275 | final Session session = _sessionFactory.getCurrentSession(); |
---|
276 | final Criteria criteria = detachedCriteria.getExecutableCriteria(session); |
---|
277 | |
---|
278 | if (null != maxResults) |
---|
279 | { |
---|
280 | criteria.setMaxResults(maxResults); |
---|
281 | if (null != firstResult) |
---|
282 | criteria.setFirstResult(firstResult); |
---|
283 | } |
---|
284 | return (List<T>) criteria.list(); |
---|
285 | } |
---|
286 | |
---|
287 | protected Integer count(final DetachedCriteria detachedCriteria) |
---|
288 | throws PersistenceException |
---|
289 | { |
---|
290 | final Session session = _sessionFactory.getCurrentSession(); |
---|
291 | final Criteria executableCriteria = detachedCriteria.getExecutableCriteria(session); |
---|
292 | executableCriteria.setProjection(Projections.rowCount()); |
---|
293 | return (Integer) executableCriteria.uniqueResult(); |
---|
294 | } |
---|
295 | // ************************************************************************** // |
---|
296 | // ************************************************************************** // |
---|
297 | |
---|
298 | @SuppressWarnings("unchecked") |
---|
299 | public final Class<DO> getDomainObjectClass() |
---|
300 | { |
---|
301 | if (_domainObjectClass == null) |
---|
302 | { |
---|
303 | if (getClass().getGenericSuperclass() instanceof ParameterizedType) |
---|
304 | _domainObjectClass = (Class<DO>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0]; |
---|
305 | else |
---|
306 | throw new IllegalArgumentException("domain class is not null and DAO is not parameterized !"); |
---|
307 | } |
---|
308 | |
---|
309 | return _domainObjectClass; |
---|
310 | } |
---|
311 | |
---|
312 | public final void setDomainObjectClass(final Class<DO> domainObjectClass) |
---|
313 | { |
---|
314 | _domainObjectClass = domainObjectClass; |
---|
315 | } |
---|
316 | |
---|
317 | public final void setSessionFactory(final SessionFactory sessionFactory) |
---|
318 | { |
---|
319 | _sessionFactory = sessionFactory; |
---|
320 | } |
---|
321 | |
---|
322 | public final SessionFactory getSessionFactory() |
---|
323 | { |
---|
324 | return _sessionFactory; |
---|
325 | } |
---|
326 | |
---|
327 | protected static final Integer MAX_RESULTS_FOR_PAGINATION = 100; |
---|
328 | |
---|
329 | private SessionFactory _sessionFactory; |
---|
330 | private Class<DO> _domainObjectClass; |
---|
331 | } |
---|