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.criterion.Criterion;
18 import org.hibernate.criterion.Order;
19 import org.hibernate.criterion.Restrictions;
20 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
23 * Generic DAO implementation.
25 * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
28 * Persistent object class
32 public abstract class GenericDAOImpl<T, PK extends Serializable> extends
33 HibernateDaoSupport implements GenericDAO<T, PK> {
35 * Persist the newInstance object into database.
38 * new object as a transient instance
39 * @return new primary key for the created persistent object
41 @SuppressWarnings("unchecked")
42 public PK create(T newInstance) {
43 return (PK) getSession().save(newInstance);
47 * Retrieve an object that was previously persisted to the database using the indicated id as primary key.
50 * primary key of an object to read
51 * @return an object found by the given key
53 @SuppressWarnings("unchecked")
55 return (T) getSession().get(getType(), id);
59 * Retrieve an object that was previously persisted to the database using the given criteria.
63 * @return an object found according to the given criteria
65 @SuppressWarnings("unchecked")
66 public T findByCriteria(Criterion aCondition) {
67 return (T) getSession().createCriteria(getType()).add(aCondition)
72 * Retrieve an object that was previously persisted to the database using the given criteria.
75 * a properties values to filter with AND condition
76 * @return an object found according to the given criteria
78 public T findByCriteria(Properties andParams) {
79 Criterion aCondition = null;
80 for (String aName: andParams.stringPropertyNames()) {
81 aCondition = Restrictions.and(aCondition, Restrictions.eq(aName, andParams.get(aName)));
83 return findByCriteria(aCondition);
87 * Retrieve a list of all objects of the considered type T which were previously persisted to the database.
89 * @return a list of all objects of the considered type T
91 @SuppressWarnings("unchecked")
92 public List<T> getAll() {
93 return getSession().createCriteria(getType()).list();
97 * Retrieve an ordered list of all objects of the considered type T which were previously persisted to the database.
100 * a result list order. Null is ignored and in such case the result list is unordered.
101 * @return an ordered list of all objects of the considered type T
103 @SuppressWarnings("unchecked")
104 public List<T> getAll(Order ... anOrder) {
105 Criteria aCriteria = getSession().createCriteria(getType());
106 for (Order order : anOrder) {
107 if (anOrder != null) {
108 aCriteria.addOrder(order);
111 return aCriteria.list();
115 * Retrieve a list of objects which were previously persisted to the database using the given criteria.
119 * @return a list of objects filtered according to the given criteria
121 @SuppressWarnings("unchecked")
122 public List<T> getFilteredList(Criterion aCondition) {
123 return getSession().createCriteria(getType()).add(aCondition).list();
127 * Retrieve a list of objects which were previously persisted to the database using the given criteria.
132 * a result list order. Null is ignored and in such case the result list is unordered.
133 * @return a list of objects filtered according to the given criteria
135 @SuppressWarnings("unchecked")
136 public List<T> getFilteredList(Criterion aCondition, Order ... anOrder) {
137 Criteria aCriteria = getSession().createCriteria(getType()).add(
139 for (Order order : anOrder) {
140 if (anOrder != null) {
141 aCriteria.addOrder(order);
144 return aCriteria.list();
148 * Retrieve a list of objects which were previously persisted to the database using the given criteria.
151 * a properties values to filter with AND condition
152 * @return a list of objects filtered according to the given criteria
154 public List<T> getFilteredList(Properties andParams) {
155 return getFilteredList(andParams);
159 * Retrieve a list of objects which were previously persisted to the database using the given criteria.
162 * a properties values to filter with AND condition
164 * a result list order. Null is ignored and in such case the result list is unordered.
165 * @return a list of objects filtered according to the given criteria
167 public List<T> getFilteredList(Properties andParams, Order ... anOrder) {
168 Criterion aCondition = null;
169 for (String aName: andParams.stringPropertyNames()) {
170 aCondition = Restrictions.and(aCondition, Restrictions.eq(aName, andParams.get(aName)));
172 return getFilteredList(aCondition, anOrder);
176 * Save changes made to a persistent object.
178 * @param transientObject
179 * transient instance of the object to update
181 public void update(T transientObject) {
182 getSession().update(transientObject);
186 * Remove an object from persistent storage in the database.
188 * @param persistentObject
189 * a persistent object to delete from the database
191 public void delete(T persistentObject) {
192 getSession().delete(persistentObject);
196 * Makes detached object persistent.
198 * @param transientObject
199 * transient instance of the object to be made persistent
201 public void persist(T transientObject) {
202 getSession().persist(transientObject);
206 * Merge detached object with persistent data.
208 * @param transientObject
209 * transient instance of the object to be merged with persistent data
210 * @return merged persistent object
212 @SuppressWarnings("unchecked")
213 public T merge(T transientObject) {
214 return (T) getSession().merge(transientObject);
218 * Get persistent object type.
220 * @return Persistent object class to be processed by this DAO
222 abstract protected Class<T> getType();