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;
15 import org.hibernate.Criteria;
16 import org.hibernate.criterion.Criterion;
17 import org.hibernate.criterion.Order;
18 import org.hibernate.criterion.Restrictions;
19 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
22 * Generic DAO implementation.
24 * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
27 * Persistent object class
31 public abstract class GenericDAOImpl<T, PK extends Serializable> extends
32 HibernateDaoSupport implements GenericDAO<T, PK> {
34 * Persist the newInstance object into database.
37 * new object as a transient instance
38 * @return new primary key for the created persistent object
40 @SuppressWarnings("unchecked")
41 public PK create(T newInstance) {
42 return (PK) getSession().save(newInstance);
46 * Retrieve an object that was previously persisted to the database using the indicated id as primary key.
49 * primary key of an object to read
50 * @return an object found by the given key
52 @SuppressWarnings("unchecked")
54 return (T) getSession().get(getType(), id);
58 * Retrieve an object that was previously persisted to the database using the given criteria.
62 * @return an object found according to the given criteria
64 @SuppressWarnings("unchecked")
65 public T findByCriteria(Criterion aCondition) {
66 return (T) getSession().createCriteria(getType()).add(aCondition)
71 * Retrieve a list of objects which were previously persisted to the database using the given criteria.
75 * @return a list of objects filtered according to the given criteria
77 @SuppressWarnings("unchecked")
78 public List<T> getFilteredList(Criterion aCondition) {
79 return getSession().createCriteria(getType()).add(aCondition).list();
83 * Retrieve a list of objects which were previously persisted to the database using the given criteria.
88 * a result list order. Null is ignored and in such case the result list is unordered.
89 * @return a list of objects filtered according to the given criteria
91 @SuppressWarnings("unchecked")
92 public List<T> getFilteredList(Criterion aCondition, Order anOrder) {
93 Criteria aCriteria = getSession().createCriteria(getType()).add(
95 if (anOrder != null) {
96 aCriteria.addOrder(anOrder);
98 return aCriteria.list();
102 * Save changes made to a persistent object.
104 * @param transientObject
105 * transient instance of the object to update
107 public void update(T transientObject) {
108 getSession().update(transientObject);
112 * Remove an object from persistent storage in the database.
114 * @param persistentObject
115 * a persistent object to delete from the database
117 public void delete(T persistentObject) {
118 getSession().delete(persistentObject);
122 * Get persistent object type.
124 * @return Persistent object class to be processed by this DAO
126 abstract protected Class<T> getType();