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 * Retrieve an object that was previously persisted to the database using the indicated id as primary key.
54 * primary key of an object to read
55 * @return an object found by the given key
57 @SuppressWarnings(UNCHECKED)
58 public T get(final PK id) {
59 return (T) getSession().get(getType(), id);
63 * Retrieve an object that was previously persisted to the database using the given criteria.
67 * @return an object found according to the given criteria
69 @SuppressWarnings(UNCHECKED)
70 public T findByCriteria(final Criterion aCondition) {
71 return (T) getSession().createCriteria(getType()).add(aCondition)
76 * Retrieve an object that was previously persisted to the database using the given criteria.
79 * a properties values to filter with AND condition
80 * @return an object found according to the given criteria
82 public T findByCriteria(final Properties andParams) {
83 Criterion aCondition = null;
84 for (String aName: andParams.stringPropertyNames()) {
85 aCondition = Restrictions.and(aCondition, Restrictions.eq(aName, andParams.get(aName)));
87 return findByCriteria(aCondition);
91 * Retrieve a list of all objects of the considered type T which were previously persisted to the database.
93 * @return a list of all objects of the considered type T
95 @SuppressWarnings(UNCHECKED)
96 public List<T> getAll() {
97 return getSession().createCriteria(getType()).list();
101 * Retrieve an ordered list of all objects of the considered type T which were previously persisted to the database.
104 * a result list order. Null is ignored and in such case the result list is unordered.
105 * @return an ordered list of all objects of the considered type T
107 @SuppressWarnings(UNCHECKED)
108 public List<T> getAll(final Order ... anOrder) {
109 Criteria aCriteria = getSession().createCriteria(getType());
110 for (Order order : anOrder) {
111 if (anOrder != null) {
112 aCriteria.addOrder(order);
115 return aCriteria.list();
119 * Retrieve a list of objects which were previously persisted to the database using the given criteria.
123 * @return a list of objects filtered according to the given criteria
125 @SuppressWarnings(UNCHECKED)
126 public List<T> getFilteredList(final Criterion aCondition) {
127 return getSession().createCriteria(getType()).add(aCondition).list();
131 * Retrieve a list of objects which were previously persisted to the database using the given criteria.
136 * a result list order. Null is ignored and in such case the result list is unordered.
137 * @return a list of objects filtered according to the given criteria
139 @SuppressWarnings(UNCHECKED)
140 public List<T> getFilteredList(final Criterion aCondition, final Order ... anOrder) {
141 Criteria aCriteria = getSession().createCriteria(getType()).add(
143 for (Order order : anOrder) {
144 if (anOrder != null) {
145 aCriteria.addOrder(order);
148 return aCriteria.list();
152 * Retrieve a list of objects which were previously persisted to the database using the given criteria.
155 * a properties values to filter with AND condition
156 * @return a list of objects filtered according to the given criteria
158 public List<T> getFilteredList(final Properties andParams) {
159 return getFilteredList(andParams);
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
168 * a result list order. Null is ignored and in such case the result list is unordered.
169 * @return a list of objects filtered according to the given criteria
171 public List<T> getFilteredList(final Properties andParams, final Order ... anOrder) {
172 Criterion aCondition = null;
173 for (String aName: andParams.stringPropertyNames()) {
174 aCondition = Restrictions.and(aCondition, Restrictions.eq(aName, andParams.get(aName)));
176 return getFilteredList(aCondition, anOrder);
180 * Save changes made to a persistent object.
182 * @param transientObject
183 * transient instance of the object to update
185 public void update(final T transientObject) {
186 getSession().update(transientObject);
190 * Remove an object from persistent storage in the database.
192 * @param persistentObject
193 * a persistent object to delete from the database
195 public void delete(final T persistentObject) {
196 getSession().delete(persistentObject);
200 * Makes detached object persistent.
202 * @param transientObject
203 * transient instance of the object to be made persistent
205 public void persist(final T transientObject) {
206 getSession().persist(transientObject);
210 * Merge detached object with persistent data.
212 * @param transientObject
213 * transient instance of the object to be merged with persistent data
214 * @return merged persistent object
216 @SuppressWarnings(UNCHECKED)
217 public T merge(final T transientObject) {
218 return (T) getSession().merge(transientObject);
222 * Synchronize the session data with the database.
224 public void flush() {
225 getSession().flush();
229 * Get persistent object type.
231 * @return Persistent object class to be processed by this DAO
233 abstract protected Class<T> getType();