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.criterion.Criterion;
19 import org.hibernate.criterion.DetachedCriteria;
20 import org.hibernate.criterion.Order;
21 import org.hibernate.criterion.Restrictions;
22 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
25 * Generic DAO implementation.
27 * @author <a href="mailto:roman.kozlov@opencascade.com">Roman Kozlov (RKV)</a>
30 * Persistent object class
34 public abstract class AbstractGenericDAOImpl<T, PK extends Serializable>
35 extends HibernateDaoSupport implements GenericDAO<T, PK> {
37 * Unchecked warning specification.
39 private static final String UNCHECKED = "unchecked";
42 * Persist the newInstance object into database.
45 * new object as a transient instance
46 * @return new primary key for the created persistent object
48 @SuppressWarnings(UNCHECKED)
49 public PK create(final T newInstance) {
50 return (PK) getSession().save(newInstance);
54 * Persist the newInstance object into database.
57 * new object as a transient instance
59 @SuppressWarnings(UNCHECKED)
60 public void saveOrUpdate(final T newInstance) {
61 getSession().saveOrUpdate(newInstance);
65 * Retrieve an object that was previously persisted to the database using the indicated id as primary key.
68 * primary key of an object to read
69 * @return an object found by the given key
71 @SuppressWarnings(UNCHECKED)
72 public T get(final PK id) {
73 return (T) getSession().get(getType(), id);
77 * Retrieve an object that was previously persisted to the database using the given criteria.
81 * @return an object found according to the given criteria
83 @SuppressWarnings(UNCHECKED)
84 public T findByCriteria(final Criterion aCondition) {
85 return (T) getSession().createCriteria(getType()).add(aCondition)
90 * Retrieve an object that was previously persisted to the database using the given criteria.
93 * a properties values to filter with AND condition
94 * @return an object found according to the given criteria
96 public T findByCriteria(final Properties andParams) {
97 Criterion aCondition = null;
98 for (String aName : andParams.stringPropertyNames()) {
99 aCondition = Restrictions.and(aCondition, Restrictions.eq(aName,
100 andParams.get(aName)));
102 return findByCriteria(aCondition);
106 * Retrieve a list of all objects of the considered type T which were previously persisted to the database.
108 * @return a list of all objects of the considered type T
110 @SuppressWarnings(UNCHECKED)
111 public List<T> getAll() {
112 return getSession().createCriteria(getType()).list();
116 * Retrieve an ordered list of all objects of the considered type T which were previously persisted to the database.
119 * a result list order. Null is ignored and in such case the result list is unordered.
120 * @return an ordered list of all objects of the considered type T
122 @SuppressWarnings(UNCHECKED)
123 public List<T> getAll(final Order... anOrder) {
124 Criteria aCriteria = getSession().createCriteria(getType());
125 for (Order order : anOrder) {
127 aCriteria.addOrder(order);
130 return aCriteria.list();
134 * Retrieve a list of objects which were previously persisted to the database using the given criteria.
136 * @param aDetachedCriteria
138 * @return a list of objects filtered according to the given criteria
140 @SuppressWarnings(UNCHECKED)
141 public List<T> getFilteredList(final DetachedCriteria aDetachedCriteria) {
142 return aDetachedCriteria.getExecutableCriteria(getSession()).list();
146 * Retrieve a list of objects which were previously persisted to the database using the given criteria.
150 * @return a list of objects filtered according to the given criteria
152 @SuppressWarnings(UNCHECKED)
153 public List<T> getFilteredList(final Criterion aCondition) {
154 return getSession().createCriteria(getType()).add(aCondition).list();
158 * Retrieve a list of objects which were previously persisted to the database using the given criteria.
163 * a result list order. Null is ignored and in such case the result list is unordered.
164 * @return a list of objects filtered according to the given criteria
166 @SuppressWarnings(UNCHECKED)
167 public List<T> getFilteredList(final Criterion aCondition,
168 final Order... anOrder) {
169 Criteria aCriteria = getSession().createCriteria(getType()).add(
171 for (Order order : anOrder) {
173 aCriteria.addOrder(order);
176 return aCriteria.list();
180 * Retrieve a list of objects which were previously persisted to the database using the given criteria.
183 * a field containing object to apply the condition
187 * @return a list of objects filtered according to the given criteria
189 public List<T> getFilteredList(final String joinField,
190 final Criterion aCondition) {
191 return getFilteredList(joinField, aCondition, (Order) null);
195 * Retrieve a list of objects which were previously persisted to the database using the given criteria.
198 * a field containing object to apply the condition
203 * a result list order. Null is ignored and in such case the result list is unordered.
204 * @return a list of objects filtered according to the given criteria
206 @SuppressWarnings(UNCHECKED)
207 public List<T> getFilteredList(final String joinField,
208 final Criterion aCondition, final Order... anOrder) {
209 Criteria aCriteria = getSession().createCriteria(getType());
210 for (Order order : anOrder) {
212 aCriteria.addOrder(order);
215 return aCriteria.createCriteria(joinField).add(aCondition).list();
219 * Retrieve a list of objects which were previously persisted to the database using the given criteria.
222 * a properties values to filter with AND condition
223 * @return a list of objects filtered according to the given criteria
225 public List<T> getFilteredList(final Properties andParams) {
226 return getFilteredList(andParams, (Order) null);
230 * Retrieve a list of objects which were previously persisted to the database using the given criteria.
233 * a properties values to filter with AND condition
235 * a result list order. Null is ignored and in such case the result list is unordered.
236 * @return a list of objects filtered according to the given criteria
238 public List<T> getFilteredList(final Properties andParams,
239 final Order... anOrder) {
240 Criterion aCondition = null;
241 for (String aName : andParams.stringPropertyNames()) {
242 aCondition = Restrictions.and(aCondition, Restrictions.eq(aName,
243 andParams.get(aName)));
245 return getFilteredList(aCondition, anOrder);
249 * Save changes made to a persistent object.
251 * @param transientObject
252 * transient instance of the object to update
254 public void update(final T transientObject) {
255 getSession().update(transientObject);
259 * Refresh a persistent object.
261 * @param transientObject
262 * transient instance of the object to refresh
264 public void refresh(final T transientObject) {
265 getSession().refresh(transientObject);
269 * Load a persistent object.
271 * @param transientObject
272 * transient instance of the object to load
276 public void load(final T transientObject, final PK id) {
277 getSession().load(transientObject, id);
281 * Lock a persistent object.
283 * @param transientObject
284 * transient instance of the object to lock
288 public void refresh(final T transientObject, final LockOptions lockOptions) {
289 getSession().refresh(transientObject, lockOptions);
293 * Remove an object from persistent storage in the database.
295 * @param persistentObject
296 * a persistent object to delete from the database
298 public void delete(final T persistentObject) {
299 getSession().delete(persistentObject);
303 * Makes detached object persistent.
305 * @param transientObject
306 * transient instance of the object to be made persistent
308 public void persist(final T transientObject) {
309 getSession().persist(transientObject);
313 * Merge detached object with persistent data.
315 * @param transientObject
316 * transient instance of the object to be merged with persistent data
317 * @return merged persistent object
319 @SuppressWarnings(UNCHECKED)
320 public T merge(final T transientObject) {
321 return (T) getSession().merge(transientObject);
325 * Synchronize the session data with the database.
327 public void flush() {
328 getSession().flush();
332 * Get persistent object type.
334 * @return Persistent object class to be processed by this DAO
336 abstract protected Class<T> getType();