1 /*****************************************************************************
5 * Creation date 08.10.2012
8 *****************************************************************************/
10 package org.splat.dal.dao.kernel;
12 import java.io.Serializable;
13 import java.util.List;
14 import java.util.Properties;
16 import org.hibernate.Criteria;
17 import org.hibernate.LockOptions;
18 import org.hibernate.Session;
19 import org.hibernate.criterion.Criterion;
20 import org.hibernate.criterion.DetachedCriteria;
21 import org.hibernate.criterion.Order;
22 import org.hibernate.criterion.Restrictions;
23 import org.splat.common.Constants;
24 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
27 * Generic DAO implementation.
29 * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
32 * Persistent object class
36 public abstract class AbstractGenericDAOImpl<T, PK extends Serializable>
37 extends HibernateDaoSupport implements GenericDAO<T, PK> {
40 * Persist the newInstance object into database.
43 * new object as a transient instance
44 * @return new primary key for the created persistent object
45 * @see Session#save(Object)
47 @SuppressWarnings(Constants.UNCHECKED)
48 public PK create(final T newInstance) {
49 return (PK) getSession().save(newInstance);
53 * Persist the newInstance object into database.
56 * new object as a transient instance
57 * @see Session#saveOrUpdate(Object)
59 @SuppressWarnings(Constants.UNCHECKED)
60 public void saveOrUpdate(final T newInstance) {
61 getSession().saveOrUpdate(newInstance);
65 * Retrieve an object that was previously persisted to the database using the indicated id as primary key.
68 * primary key of an object to read
69 * @return an object found by the given key
70 * @see Session#get(Class, Serializable)
72 @SuppressWarnings(Constants.UNCHECKED)
73 public T get(final PK id) {
74 return (T) getSession().get(getType(), id);
78 * Retrieve an object that was previously persisted to the database using the given criteria.
82 * @return an object found according to the given criteria
84 @SuppressWarnings(Constants.UNCHECKED)
85 public T findByCriteria(final Criterion aCondition) {
86 return (T) getSession().createCriteria(getType()).add(aCondition)
91 * Retrieve an object that was previously persisted to the database using the given criteria.
94 * a properties values to filter with AND condition
95 * @return an object found according to the given criteria
97 public T findByCriteria(final Properties andParams) {
98 Criterion aCondition = null;
99 for (String aName : andParams.stringPropertyNames()) {
100 aCondition = Restrictions.and(aCondition, Restrictions.eq(aName,
101 andParams.get(aName)));
103 return findByCriteria(aCondition);
107 * Retrieve a list of all objects of the considered type T which were previously persisted to the database.
109 * @return a list of all objects of the considered type T
111 @SuppressWarnings(Constants.UNCHECKED)
112 public List<T> getAll() {
113 return getSession().createCriteria(getType()).list();
117 * Retrieve an ordered list of all objects of the considered type T which were previously persisted to the database.
120 * a result list order. Null is ignored and in such case the result list is unordered.
121 * @return an ordered list of all objects of the considered type T
123 @SuppressWarnings(Constants.UNCHECKED)
124 public List<T> getAll(final Order... anOrder) {
125 Criteria aCriteria = getSession().createCriteria(getType());
126 for (Order order : anOrder) {
128 aCriteria.addOrder(order);
131 return aCriteria.list();
135 * Retrieve a list of objects which were previously persisted to the database using the given criteria.
137 * @param aDetachedCriteria
139 * @return a list of objects filtered according to the given criteria
141 @SuppressWarnings(Constants.UNCHECKED)
142 public List<T> getFilteredList(final DetachedCriteria aDetachedCriteria) {
143 return aDetachedCriteria.getExecutableCriteria(getSession()).list();
147 * Retrieve a first found object in the database using the given criteria.
149 * @param aDetachedCriteria
151 * @return a first found object filtered according to the given criteria
153 @SuppressWarnings(Constants.UNCHECKED)
154 public T getFirstResult(final DetachedCriteria aDetachedCriteria) {
155 return (T) aDetachedCriteria.getExecutableCriteria(getSession())
156 .setMaxResults(1).uniqueResult();
160 * Retrieve a list of DTO objects using the given criteria.
163 * the class of returned DTOs
164 * @param aDetachedCriteria
166 * @return a list of DTO objects filtered according to the given criteria
168 @SuppressWarnings(Constants.UNCHECKED)
169 public <DTO> List<DTO> getFilteredDTOList(
170 final DetachedCriteria aDetachedCriteria) {
171 return aDetachedCriteria.getExecutableCriteria(getSession()).list();
175 * Retrieve a list of objects which were previously persisted to the database using the given criteria.
179 * @return a list of objects filtered according to the given criteria
181 @SuppressWarnings(Constants.UNCHECKED)
182 public List<T> getFilteredList(final Criterion aCondition) {
183 return getSession().createCriteria(getType()).add(aCondition).list();
187 * Retrieve a list of objects which were previously persisted to the database using the given criteria.
192 * a result list order. Null is ignored and in such case the result list is unordered.
193 * @return a list of objects filtered according to the given criteria
195 @SuppressWarnings(Constants.UNCHECKED)
196 public List<T> getFilteredList(final Criterion aCondition,
197 final Order... anOrder) {
198 Criteria aCriteria = getSession().createCriteria(getType()).add(
200 for (Order order : anOrder) {
202 aCriteria.addOrder(order);
205 return aCriteria.list();
209 * Retrieve a list of objects which were previously persisted to the database using the given criteria.
212 * a field containing object to apply the condition
216 * @return a list of objects filtered according to the given criteria
218 public List<T> getFilteredList(final String joinField,
219 final Criterion aCondition) {
220 return getFilteredList(joinField, aCondition, (Order) null);
224 * Retrieve a list of objects which were previously persisted to the database using the given criteria.
227 * a field containing object to apply the condition
232 * a result list order. Null is ignored and in such case the result list is unordered.
233 * @return a list of objects filtered according to the given criteria
235 @SuppressWarnings(Constants.UNCHECKED)
236 public List<T> getFilteredList(final String joinField,
237 final Criterion aCondition, final Order... anOrder) {
238 Criteria aCriteria = getSession().createCriteria(getType());
239 for (Order order : anOrder) {
241 aCriteria.addOrder(order);
244 return aCriteria.createCriteria(joinField).add(aCondition).list();
248 * Retrieve a list of objects which were previously persisted to the database using the given criteria.
251 * a properties values to filter with AND condition
252 * @return a list of objects filtered according to the given criteria
254 public List<T> getFilteredList(final Properties andParams) {
255 return getFilteredList(andParams, (Order) null);
259 * Retrieve a list of objects which were previously persisted to the database using the given criteria.
262 * a properties values to filter with AND condition
264 * a result list order. Null is ignored and in such case the result list is unordered.
265 * @return a list of objects filtered according to the given criteria
267 public List<T> getFilteredList(final Properties andParams,
268 final Order... anOrder) {
269 Criterion aCondition = null;
270 for (String aName : andParams.stringPropertyNames()) {
271 aCondition = Restrictions.and(aCondition, Restrictions.eq(aName,
272 andParams.get(aName)));
274 return getFilteredList(aCondition, anOrder);
278 * Update the persistent instance with the identifier of the given detached <BR>
279 * instance. If there is a persistent instance with the same identifier, an <BR>
280 * exception is thrown. This operation cascades to associated instances if <BR>
281 * the association is mapped with cascade="save-update".
283 * @param detachedObject
284 * transient instance of the object to update
285 * @see Session#update(Object)
287 public void update(final T detachedObject) {
288 getSession().update(detachedObject);
292 * Re-read the state of the given instance from the underlying database. <BR>
293 * It is inadvisable to use this to implement long-running sessions that <BR>
294 * span many business tasks. This method is, however, useful in certain <BR>
295 * special circumstances. For example
297 * <li>where a database trigger alters the object state upon insert or update</li>
298 * <li>after executing direct SQL (eg. a mass update) in the same session</li>
299 * <li>after inserting a Blob or Clob</li>
303 * a persistent or detached instance to refresh
304 * @see Session#refresh(Object)
306 public void refresh(final T anObject) {
307 getSession().refresh(anObject);
311 * Load a persistent object.
313 * @param transientObject
314 * transient instance of the object to load
317 * @see Session#load(Object, Serializable)
319 public void load(final T transientObject, final PK id) {
320 getSession().load(transientObject, id);
324 * Lock a persistent object.
326 * @param transientObject
327 * transient instance of the object to lock
330 * @see Session#refresh(Object, LockOptions)
332 public void refresh(final T transientObject, final LockOptions lockOptions) {
333 getSession().refresh(transientObject, lockOptions);
337 * Remove an object from persistent storage in the database.
339 * @param persistentObject
340 * a persistent object to delete from the database
341 * @see Session#delete(Object)
343 public void delete(final T persistentObject) {
344 getSession().delete(persistentObject);
348 * Make a transient instance persistent. This operation cascades to <BR>
349 * associated instances if the association is mapped with cascade="persist".
351 * @param transientObject
352 * transient instance of the object to be made persistent
353 * @see Session#persist(Object)
355 public void persist(final T transientObject) {
356 getSession().persist(transientObject);
360 * Copy the state of the given object onto the persistent object with the <BR>
361 * same identifier. If there is no persistent instance currently associated<BR>
362 * with the session, it will be loaded. Return the persistent instance. If <BR>
363 * the given instance is unsaved, save a copy of and return it as a newly <BR>
364 * persistent instance. The given instance does not become associated with <BR>
365 * the session. This operation cascades to associated instances if the <BR>
366 * association is mapped with cascade="merge".
368 * @param transientObject
369 * transient instance of the object to be merged with persistent data
370 * @return merged persistent object
371 * @see Session#merge(Object)
373 @SuppressWarnings(Constants.UNCHECKED)
374 public T merge(final T transientObject) {
375 return (T) getSession().merge(transientObject);
379 * Remove this instance from the session cache. Changes to the instance will<BR>
380 * not be synchronized with the database. This operation cascades to <BR>
381 * associated instances if the association is mapped with cascade="evict".
383 * @param persistentObject
384 * the object to be removed from session cache
385 * @see Session#evict(Object)
387 public void evict(final T persistentObject) {
388 getSession().evict(persistentObject);
392 * Synchronize the session data with the database.
394 * @see Session#flush()
396 public void flush() {
397 getSession().flush();
401 * Get persistent object type.
403 * @return Persistent object class to be processed by this DAO
405 abstract protected Class<T> getType();