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 list of DTO objects using the given criteria.
150 * the class of returned DTOs
151 * @param aDetachedCriteria
153 * @return a list of DTO objects filtered according to the given criteria
155 @SuppressWarnings(Constants.UNCHECKED)
156 public <DTO> List<DTO> getFilteredDTOList(
157 final DetachedCriteria aDetachedCriteria) {
158 return aDetachedCriteria.getExecutableCriteria(getSession()).list();
162 * Retrieve a list of objects which were previously persisted to the database using the given criteria.
166 * @return a list of objects filtered according to the given criteria
168 @SuppressWarnings(Constants.UNCHECKED)
169 public List<T> getFilteredList(final Criterion aCondition) {
170 return getSession().createCriteria(getType()).add(aCondition).list();
174 * Retrieve a list of objects which were previously persisted to the database using the given criteria.
179 * a result list order. Null is ignored and in such case the result list is unordered.
180 * @return a list of objects filtered according to the given criteria
182 @SuppressWarnings(Constants.UNCHECKED)
183 public List<T> getFilteredList(final Criterion aCondition,
184 final Order... anOrder) {
185 Criteria aCriteria = getSession().createCriteria(getType()).add(
187 for (Order order : anOrder) {
189 aCriteria.addOrder(order);
192 return aCriteria.list();
196 * Retrieve a list of objects which were previously persisted to the database using the given criteria.
199 * a field containing object to apply the condition
203 * @return a list of objects filtered according to the given criteria
205 public List<T> getFilteredList(final String joinField,
206 final Criterion aCondition) {
207 return getFilteredList(joinField, aCondition, (Order) null);
211 * Retrieve a list of objects which were previously persisted to the database using the given criteria.
214 * a field containing object to apply the condition
219 * a result list order. Null is ignored and in such case the result list is unordered.
220 * @return a list of objects filtered according to the given criteria
222 @SuppressWarnings(Constants.UNCHECKED)
223 public List<T> getFilteredList(final String joinField,
224 final Criterion aCondition, final Order... anOrder) {
225 Criteria aCriteria = getSession().createCriteria(getType());
226 for (Order order : anOrder) {
228 aCriteria.addOrder(order);
231 return aCriteria.createCriteria(joinField).add(aCondition).list();
235 * Retrieve a list of objects which were previously persisted to the database using the given criteria.
238 * a properties values to filter with AND condition
239 * @return a list of objects filtered according to the given criteria
241 public List<T> getFilteredList(final Properties andParams) {
242 return getFilteredList(andParams, (Order) null);
246 * Retrieve a list of objects which were previously persisted to the database using the given criteria.
249 * a properties values to filter with AND condition
251 * a result list order. Null is ignored and in such case the result list is unordered.
252 * @return a list of objects filtered according to the given criteria
254 public List<T> getFilteredList(final Properties andParams,
255 final Order... anOrder) {
256 Criterion aCondition = null;
257 for (String aName : andParams.stringPropertyNames()) {
258 aCondition = Restrictions.and(aCondition, Restrictions.eq(aName,
259 andParams.get(aName)));
261 return getFilteredList(aCondition, anOrder);
265 * Update the persistent instance with the identifier of the given detached <BR>
266 * instance. If there is a persistent instance with the same identifier, an <BR>
267 * exception is thrown. This operation cascades to associated instances if <BR>
268 * the association is mapped with cascade="save-update".
270 * @param detachedObject
271 * transient instance of the object to update
272 * @see Session#update(Object)
274 public void update(final T detachedObject) {
275 getSession().update(detachedObject);
279 * Re-read the state of the given instance from the underlying database. <BR>
280 * It is inadvisable to use this to implement long-running sessions that <BR>
281 * span many business tasks. This method is, however, useful in certain <BR>
282 * special circumstances. For example
284 * <li>where a database trigger alters the object state upon insert or update</li>
285 * <li>after executing direct SQL (eg. a mass update) in the same session</li>
286 * <li>after inserting a Blob or Clob</li>
290 * a persistent or detached instance to refresh
291 * @see Session#refresh(Object)
293 public void refresh(final T anObject) {
294 getSession().refresh(anObject);
298 * Load a persistent object.
300 * @param transientObject
301 * transient instance of the object to load
304 * @see Session#load(Object, Serializable)
306 public void load(final T transientObject, final PK id) {
307 getSession().load(transientObject, id);
311 * Lock a persistent object.
313 * @param transientObject
314 * transient instance of the object to lock
317 * @see Session#refresh(Object, LockOptions)
319 public void refresh(final T transientObject, final LockOptions lockOptions) {
320 getSession().refresh(transientObject, lockOptions);
324 * Remove an object from persistent storage in the database.
326 * @param persistentObject
327 * a persistent object to delete from the database
328 * @see Session#delete(Object)
330 public void delete(final T persistentObject) {
331 getSession().delete(persistentObject);
335 * Make a transient instance persistent. This operation cascades to <BR>
336 * associated instances if the association is mapped with cascade="persist".
338 * @param transientObject
339 * transient instance of the object to be made persistent
340 * @see Session#persist(Object)
342 public void persist(final T transientObject) {
343 getSession().persist(transientObject);
347 * Copy the state of the given object onto the persistent object with the <BR>
348 * same identifier. If there is no persistent instance currently associated<BR>
349 * with the session, it will be loaded. Return the persistent instance. If <BR>
350 * the given instance is unsaved, save a copy of and return it as a newly <BR>
351 * persistent instance. The given instance does not become associated with <BR>
352 * the session. This operation cascades to associated instances if the <BR>
353 * association is mapped with cascade="merge".
355 * @param transientObject
356 * transient instance of the object to be merged with persistent data
357 * @return merged persistent object
358 * @see Session#merge(Object)
360 @SuppressWarnings(Constants.UNCHECKED)
361 public T merge(final T transientObject) {
362 return (T) getSession().merge(transientObject);
366 * Remove this instance from the session cache. Changes to the instance will<BR>
367 * not be synchronized with the database. This operation cascades to <BR>
368 * associated instances if the association is mapped with cascade="evict".
370 * @param persistentObject
371 * the object to be removed from session cache
372 * @see Session#evict(Object)
374 public void evict(final T persistentObject) {
375 getSession().evict(persistentObject);
379 * Synchronize the session data with the database.
381 * @see Session#flush()
383 public void flush() {
384 getSession().flush();
388 * Get persistent object type.
390 * @return Persistent object class to be processed by this DAO
392 abstract protected Class<T> getType();