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.springframework.orm.hibernate3.support.HibernateDaoSupport;
26 * Generic DAO implementation.
28 * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
31 * Persistent object class
35 public abstract class AbstractGenericDAOImpl<T, PK extends Serializable>
36 extends HibernateDaoSupport implements GenericDAO<T, PK> {
38 * Unchecked warning specification.
40 private static final String UNCHECKED = "unchecked";
43 * Persist the newInstance object into database.
46 * new object as a transient instance
47 * @return new primary key for the created persistent object
48 * @see Session#save(Object)
50 @SuppressWarnings(UNCHECKED)
51 public PK create(final T newInstance) {
52 return (PK) getSession().save(newInstance);
56 * Persist the newInstance object into database.
59 * new object as a transient instance
60 * @see Session#saveOrUpdate(Object)
62 @SuppressWarnings(UNCHECKED)
63 public void saveOrUpdate(final T newInstance) {
64 getSession().saveOrUpdate(newInstance);
68 * Retrieve an object that was previously persisted to the database using the indicated id as primary key.
71 * primary key of an object to read
72 * @return an object found by the given key
73 * @see Session#get(Class, Serializable)
75 @SuppressWarnings(UNCHECKED)
76 public T get(final PK id) {
77 return (T) getSession().get(getType(), id);
81 * Retrieve an object that was previously persisted to the database using the given criteria.
85 * @return an object found according to the given criteria
87 @SuppressWarnings(UNCHECKED)
88 public T findByCriteria(final Criterion aCondition) {
89 return (T) getSession().createCriteria(getType()).add(aCondition)
94 * Retrieve an object that was previously persisted to the database using the given criteria.
97 * a properties values to filter with AND condition
98 * @return an object found according to the given criteria
100 public T findByCriteria(final Properties andParams) {
101 Criterion aCondition = null;
102 for (String aName : andParams.stringPropertyNames()) {
103 aCondition = Restrictions.and(aCondition, Restrictions.eq(aName,
104 andParams.get(aName)));
106 return findByCriteria(aCondition);
110 * Retrieve a list of all objects of the considered type T which were previously persisted to the database.
112 * @return a list of all objects of the considered type T
114 @SuppressWarnings(UNCHECKED)
115 public List<T> getAll() {
116 return getSession().createCriteria(getType()).list();
120 * Retrieve an ordered list of all objects of the considered type T which were previously persisted to the database.
123 * a result list order. Null is ignored and in such case the result list is unordered.
124 * @return an ordered list of all objects of the considered type T
126 @SuppressWarnings(UNCHECKED)
127 public List<T> getAll(final Order... anOrder) {
128 Criteria aCriteria = getSession().createCriteria(getType());
129 for (Order order : anOrder) {
131 aCriteria.addOrder(order);
134 return aCriteria.list();
138 * Retrieve a list of objects which were previously persisted to the database using the given criteria.
140 * @param aDetachedCriteria
142 * @return a list of objects filtered according to the given criteria
144 @SuppressWarnings(UNCHECKED)
145 public List<T> getFilteredList(final DetachedCriteria aDetachedCriteria) {
146 return aDetachedCriteria.getExecutableCriteria(getSession()).list();
150 * Retrieve a list of DTO objects using the given criteria.
153 * the class of returned DTOs
154 * @param aDetachedCriteria
156 * @return a list of DTO objects filtered according to the given criteria
158 @SuppressWarnings(UNCHECKED)
159 public <DTO> List<DTO> getFilteredDTOList(
160 final DetachedCriteria aDetachedCriteria) {
161 return aDetachedCriteria.getExecutableCriteria(getSession()).list();
165 * Retrieve a list of objects which were previously persisted to the database using the given criteria.
169 * @return a list of objects filtered according to the given criteria
171 @SuppressWarnings(UNCHECKED)
172 public List<T> getFilteredList(final Criterion aCondition) {
173 return getSession().createCriteria(getType()).add(aCondition).list();
177 * Retrieve a list of objects which were previously persisted to the database using the given criteria.
182 * a result list order. Null is ignored and in such case the result list is unordered.
183 * @return a list of objects filtered according to the given criteria
185 @SuppressWarnings(UNCHECKED)
186 public List<T> getFilteredList(final Criterion aCondition,
187 final Order... anOrder) {
188 Criteria aCriteria = getSession().createCriteria(getType()).add(
190 for (Order order : anOrder) {
192 aCriteria.addOrder(order);
195 return aCriteria.list();
199 * Retrieve a list of objects which were previously persisted to the database using the given criteria.
202 * a field containing object to apply the condition
206 * @return a list of objects filtered according to the given criteria
208 public List<T> getFilteredList(final String joinField,
209 final Criterion aCondition) {
210 return getFilteredList(joinField, aCondition, (Order) null);
214 * Retrieve a list of objects which were previously persisted to the database using the given criteria.
217 * a field containing object to apply the condition
222 * a result list order. Null is ignored and in such case the result list is unordered.
223 * @return a list of objects filtered according to the given criteria
225 @SuppressWarnings(UNCHECKED)
226 public List<T> getFilteredList(final String joinField,
227 final Criterion aCondition, final Order... anOrder) {
228 Criteria aCriteria = getSession().createCriteria(getType());
229 for (Order order : anOrder) {
231 aCriteria.addOrder(order);
234 return aCriteria.createCriteria(joinField).add(aCondition).list();
238 * Retrieve a list of objects which were previously persisted to the database using the given criteria.
241 * a properties values to filter with AND condition
242 * @return a list of objects filtered according to the given criteria
244 public List<T> getFilteredList(final Properties andParams) {
245 return getFilteredList(andParams, (Order) null);
249 * Retrieve a list of objects which were previously persisted to the database using the given criteria.
252 * a properties values to filter with AND condition
254 * a result list order. Null is ignored and in such case the result list is unordered.
255 * @return a list of objects filtered according to the given criteria
257 public List<T> getFilteredList(final Properties andParams,
258 final Order... anOrder) {
259 Criterion aCondition = null;
260 for (String aName : andParams.stringPropertyNames()) {
261 aCondition = Restrictions.and(aCondition, Restrictions.eq(aName,
262 andParams.get(aName)));
264 return getFilteredList(aCondition, anOrder);
268 * Save changes made to a persistent object.
270 * @param transientObject
271 * transient instance of the object to update
273 public void update(final T transientObject) {
274 getSession().update(transientObject);
278 * Refresh a persistent object.
280 * @param transientObject
281 * transient instance of the object to refresh
282 * @see Session#refresh(Object)
284 public void refresh(final T transientObject) {
285 getSession().refresh(transientObject);
289 * Load a persistent object.
291 * @param transientObject
292 * transient instance of the object to load
295 * @see Session#load(Object, Serializable)
297 public void load(final T transientObject, final PK id) {
298 getSession().load(transientObject, id);
302 * Lock a persistent object.
304 * @param transientObject
305 * transient instance of the object to lock
308 * @see Session#refresh(Object, LockOptions)
310 public void refresh(final T transientObject, final LockOptions lockOptions) {
311 getSession().refresh(transientObject, lockOptions);
315 * Remove an object from persistent storage in the database.
317 * @param persistentObject
318 * a persistent object to delete from the database
319 * @see Session#delete(Object)
321 public void delete(final T persistentObject) {
322 getSession().delete(persistentObject);
326 * Make a transient instance persistent. This operation cascades to <BR>
327 * associated instances if the association is mapped with cascade="persist".
329 * @param transientObject
330 * transient instance of the object to be made persistent
331 * @see Session#persist(Object)
333 public void persist(final T transientObject) {
334 getSession().persist(transientObject);
338 * Copy the state of the given object onto the persistent object with the <BR>
339 * same identifier. If there is no persistent instance currently associated<BR>
340 * with the session, it will be loaded. Return the persistent instance. If <BR>
341 * the given instance is unsaved, save a copy of and return it as a newly <BR>
342 * persistent instance. The given instance does not become associated with <BR>
343 * the session. This operation cascades to associated instances if the <BR>
344 * association is mapped with cascade="merge".
346 * @param transientObject
347 * transient instance of the object to be merged with persistent data
348 * @return merged persistent object
349 * @see Session#merge(Object)
351 @SuppressWarnings(UNCHECKED)
352 public T merge(final T transientObject) {
353 return (T) getSession().merge(transientObject);
357 * Remove this instance from the session cache. Changes to the instance will<BR>
358 * not be synchronized with the database. This operation cascades to <BR>
359 * associated instances if the association is mapped with cascade="evict".
361 * @param persistentObject
362 * the object to be removed from session cache
363 * @see Session#evict(Object)
365 @SuppressWarnings(UNCHECKED)
366 public void evict(final T persistentObject) {
367 getSession().evict(persistentObject);
371 * Synchronize the session data with the database.
373 * @see Session#flush()
375 public void flush() {
376 getSession().flush();
380 * Get persistent object type.
382 * @return Persistent object class to be processed by this DAO
384 abstract protected Class<T> getType();