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 AbstractGenericDAOImpl<T, PK extends Serializable> extends
33 HibernateDaoSupport implements GenericDAO<T, PK> {
35 * Unchecked warning specification.
37 private static final String UNCHECKED = "unchecked";
39 * Persist the newInstance object into database.
42 * new object as a transient instance
43 * @return new primary key for the created persistent object
45 @SuppressWarnings(UNCHECKED)
46 public PK create(final T newInstance) {
47 return (PK) getSession().save(newInstance);
51 * Persist the newInstance object into database.
54 * new object as a transient instance
56 @SuppressWarnings(UNCHECKED)
57 public void saveOrUpdate(final T newInstance) {
58 getSession().saveOrUpdate(newInstance);
62 * Retrieve an object that was previously persisted to the database using the indicated id as primary key.
65 * primary key of an object to read
66 * @return an object found by the given key
68 @SuppressWarnings(UNCHECKED)
69 public T get(final PK id) {
70 return (T) getSession().get(getType(), id);
74 * Retrieve an object that was previously persisted to the database using the given criteria.
78 * @return an object found according to the given criteria
80 @SuppressWarnings(UNCHECKED)
81 public T findByCriteria(final Criterion aCondition) {
82 return (T) getSession().createCriteria(getType()).add(aCondition)
87 * Retrieve an object that was previously persisted to the database using the given criteria.
90 * a properties values to filter with AND condition
91 * @return an object found according to the given criteria
93 public T findByCriteria(final Properties andParams) {
94 Criterion aCondition = null;
95 for (String aName: andParams.stringPropertyNames()) {
96 aCondition = Restrictions.and(aCondition, Restrictions.eq(aName, andParams.get(aName)));
98 return findByCriteria(aCondition);
102 * Retrieve a list of all objects of the considered type T which were previously persisted to the database.
104 * @return a list of all objects of the considered type T
106 @SuppressWarnings(UNCHECKED)
107 public List<T> getAll() {
108 return getSession().createCriteria(getType()).list();
112 * Retrieve an ordered list of all objects of the considered type T which were previously persisted to the database.
115 * a result list order. Null is ignored and in such case the result list is unordered.
116 * @return an ordered list of all objects of the considered type T
118 @SuppressWarnings(UNCHECKED)
119 public List<T> getAll(final Order ... anOrder) {
120 Criteria aCriteria = getSession().createCriteria(getType());
121 for (Order order : anOrder) {
122 if (anOrder != null) {
123 aCriteria.addOrder(order);
126 return aCriteria.list();
130 * Retrieve a list of objects which were previously persisted to the database using the given criteria.
134 * @return a list of objects filtered according to the given criteria
136 @SuppressWarnings(UNCHECKED)
137 public List<T> getFilteredList(final Criterion aCondition) {
138 return getSession().createCriteria(getType()).add(aCondition).list();
142 * Retrieve a list of objects which were previously persisted to the database using the given criteria.
147 * a result list order. Null is ignored and in such case the result list is unordered.
148 * @return a list of objects filtered according to the given criteria
150 @SuppressWarnings(UNCHECKED)
151 public List<T> getFilteredList(final Criterion aCondition, final Order ... anOrder) {
152 Criteria aCriteria = getSession().createCriteria(getType()).add(
154 for (Order order : anOrder) {
155 if (anOrder != null) {
156 aCriteria.addOrder(order);
159 return aCriteria.list();
163 * Retrieve a list of objects which were previously persisted to the database using the given criteria.
166 * a properties values to filter with AND condition
167 * @return a list of objects filtered according to the given criteria
169 public List<T> getFilteredList(final Properties andParams) {
170 return getFilteredList(andParams);
174 * Retrieve a list of objects which were previously persisted to the database using the given criteria.
177 * a properties values to filter with AND condition
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 public List<T> getFilteredList(final Properties andParams, final Order ... anOrder) {
183 Criterion aCondition = null;
184 for (String aName: andParams.stringPropertyNames()) {
185 aCondition = Restrictions.and(aCondition, Restrictions.eq(aName, andParams.get(aName)));
187 return getFilteredList(aCondition, anOrder);
191 * Save changes made to a persistent object.
193 * @param transientObject
194 * transient instance of the object to update
196 public void update(final T transientObject) {
197 getSession().update(transientObject);
201 * Remove an object from persistent storage in the database.
203 * @param persistentObject
204 * a persistent object to delete from the database
206 public void delete(final T persistentObject) {
207 getSession().delete(persistentObject);
211 * Makes detached object persistent.
213 * @param transientObject
214 * transient instance of the object to be made persistent
216 public void persist(final T transientObject) {
217 getSession().persist(transientObject);
221 * Merge detached object with persistent data.
223 * @param transientObject
224 * transient instance of the object to be merged with persistent data
225 * @return merged persistent object
227 @SuppressWarnings(UNCHECKED)
228 public T merge(final T transientObject) {
229 return (T) getSession().merge(transientObject);
233 * Synchronize the session data with the database.
235 public void flush() {
236 getSession().flush();
240 * Get persistent object type.
242 * @return Persistent object class to be processed by this DAO
244 abstract protected Class<T> getType();