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>
33 extends HibernateDaoSupport implements GenericDAO<T, PK> {
35 * Unchecked warning specification.
37 private static final String UNCHECKED = "unchecked";
40 * Persist the newInstance object into database.
43 * new object as a transient instance
44 * @return new primary key for the created persistent object
46 @SuppressWarnings(UNCHECKED)
47 public PK create(final T newInstance) {
48 return (PK) getSession().save(newInstance);
52 * Persist the newInstance object into database.
55 * new object as a transient instance
57 @SuppressWarnings(UNCHECKED)
58 public void saveOrUpdate(final T newInstance) {
59 getSession().saveOrUpdate(newInstance);
63 * Retrieve an object that was previously persisted to the database using the indicated id as primary key.
66 * primary key of an object to read
67 * @return an object found by the given key
69 @SuppressWarnings(UNCHECKED)
70 public T get(final PK id) {
71 return (T) getSession().get(getType(), id);
75 * Retrieve an object that was previously persisted to the database using the given criteria.
79 * @return an object found according to the given criteria
81 @SuppressWarnings(UNCHECKED)
82 public T findByCriteria(final Criterion aCondition) {
83 return (T) getSession().createCriteria(getType()).add(aCondition)
88 * Retrieve an object that was previously persisted to the database using the given criteria.
91 * a properties values to filter with AND condition
92 * @return an object found according to the given criteria
94 public T findByCriteria(final Properties andParams) {
95 Criterion aCondition = null;
96 for (String aName : andParams.stringPropertyNames()) {
97 aCondition = Restrictions.and(aCondition, Restrictions.eq(aName,
98 andParams.get(aName)));
100 return findByCriteria(aCondition);
104 * Retrieve a list of all objects of the considered type T which were previously persisted to the database.
106 * @return a list of all objects of the considered type T
108 @SuppressWarnings(UNCHECKED)
109 public List<T> getAll() {
110 return getSession().createCriteria(getType()).list();
114 * Retrieve an ordered list of all objects of the considered type T which were previously persisted to the database.
117 * a result list order. Null is ignored and in such case the result list is unordered.
118 * @return an ordered list of all objects of the considered type T
120 @SuppressWarnings(UNCHECKED)
121 public List<T> getAll(final Order... anOrder) {
122 Criteria aCriteria = getSession().createCriteria(getType());
123 for (Order order : anOrder) {
125 aCriteria.addOrder(order);
128 return aCriteria.list();
132 * Retrieve a list of objects which were previously persisted to the database using the given criteria.
136 * @return a list of objects filtered according to the given criteria
138 @SuppressWarnings(UNCHECKED)
139 public List<T> getFilteredList(final Criterion aCondition) {
140 return getSession().createCriteria(getType()).add(aCondition).list();
144 * Retrieve a list of objects which were previously persisted to the database using the given criteria.
149 * a result list order. Null is ignored and in such case the result list is unordered.
150 * @return a list of objects filtered according to the given criteria
152 @SuppressWarnings(UNCHECKED)
153 public List<T> getFilteredList(final Criterion aCondition,
154 final Order... anOrder) {
155 Criteria aCriteria = getSession().createCriteria(getType()).add(
157 for (Order order : anOrder) {
159 aCriteria.addOrder(order);
162 return aCriteria.list();
166 * Retrieve a list of objects which were previously persisted to the database using the given criteria.
169 * a field containing object to apply the condition
173 * @return a list of objects filtered according to the given criteria
175 public List<T> getFilteredList(final String joinField,
176 final Criterion aCondition) {
177 return getFilteredList(joinField, aCondition, (Order) null);
181 * Retrieve a list of objects which were previously persisted to the database using the given criteria.
184 * a field containing object to apply the condition
189 * a result list order. Null is ignored and in such case the result list is unordered.
190 * @return a list of objects filtered according to the given criteria
192 @SuppressWarnings(UNCHECKED)
193 public List<T> getFilteredList(final String joinField,
194 final Criterion aCondition, final Order... anOrder) {
195 Criteria aCriteria = getSession().createCriteria(getType());
196 for (Order order : anOrder) {
198 aCriteria.addOrder(order);
201 return aCriteria.createCriteria(joinField).add(aCondition).list();
205 * Retrieve a list of objects which were previously persisted to the database using the given criteria.
208 * a properties values to filter with AND condition
209 * @return a list of objects filtered according to the given criteria
211 public List<T> getFilteredList(final Properties andParams) {
212 return getFilteredList(andParams, (Order) null);
216 * Retrieve a list of objects which were previously persisted to the database using the given criteria.
219 * a properties values to filter with AND condition
221 * a result list order. Null is ignored and in such case the result list is unordered.
222 * @return a list of objects filtered according to the given criteria
224 public List<T> getFilteredList(final Properties andParams,
225 final Order... anOrder) {
226 Criterion aCondition = null;
227 for (String aName : andParams.stringPropertyNames()) {
228 aCondition = Restrictions.and(aCondition, Restrictions.eq(aName,
229 andParams.get(aName)));
231 return getFilteredList(aCondition, anOrder);
235 * Save changes made to a persistent object.
237 * @param transientObject
238 * transient instance of the object to update
240 public void update(final T transientObject) {
241 getSession().update(transientObject);
245 * Remove an object from persistent storage in the database.
247 * @param persistentObject
248 * a persistent object to delete from the database
250 public void delete(final T persistentObject) {
251 getSession().delete(persistentObject);
255 * Makes detached object persistent.
257 * @param transientObject
258 * transient instance of the object to be made persistent
260 public void persist(final T transientObject) {
261 getSession().persist(transientObject);
265 * Merge detached object with persistent data.
267 * @param transientObject
268 * transient instance of the object to be merged with persistent data
269 * @return merged persistent object
271 @SuppressWarnings(UNCHECKED)
272 public T merge(final T transientObject) {
273 return (T) getSession().merge(transientObject);
277 * Synchronize the session data with the database.
279 public void flush() {
280 getSession().flush();
284 * Get persistent object type.
286 * @return Persistent object class to be processed by this DAO
288 abstract protected Class<T> getType();