1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package net.sf.oness.common.model.dao.hibernate;
17
18 import java.io.Serializable;
19 import java.lang.reflect.InvocationTargetException;
20 import java.util.Collection;
21 import java.util.List;
22
23 import net.sf.hibernate.HibernateException;
24 import net.sf.hibernate.Session;
25 import net.sf.hibernate.expression.Expression;
26 import net.sf.hibernate.expression.MatchMode;
27 import net.sf.oness.common.model.util.CollectionCloneConverter;
28
29 import org.apache.commons.beanutils.BeanUtils;
30 import org.apache.commons.beanutils.ConvertUtils;
31 import org.springframework.orm.ObjectRetrievalFailureException;
32 import org.springframework.orm.hibernate.HibernateCallback;
33
34 /***
35 * Class with support for common Hibernate operations. Hibernate DAOs should
36 * extend this class.
37 *
38 * @author Carlos Sanchez
39 * @version $Revision: 1.9 $
40 */
41 public class HibernateDaoSupport extends
42 org.springframework.orm.hibernate.support.HibernateDaoSupport {
43
44 static {
45 ConvertUtils.register(new CollectionCloneConverter(), Collection.class);
46 }
47
48 /***
49 * Return a clone of the persistent instance with the given identifier,
50 * assuming that the instance exists.
51 *
52 * @param theClass
53 * class of the persistent object
54 * @param id
55 * a valid identifier of an existing persistent instance of the
56 * class
57 * @return the cloned object
58 */
59 protected Object loadAndClone(Class theClass, Serializable id) {
60 try {
61 return BeanUtils.cloneBean(getHibernateTemplate()
62 .load(theClass, id));
63 } catch (InstantiationException e) {
64 throw new ObjectRetrievalFailureException(theClass, id, e
65 .getMessage(), e);
66 } catch (IllegalAccessException e) {
67 throw new ObjectRetrievalFailureException(theClass, id, e
68 .getMessage(), e);
69 } catch (InvocationTargetException e) {
70 throw new ObjectRetrievalFailureException(theClass, id, e
71 .getMessage(), e);
72 } catch (NoSuchMethodException e) {
73 throw new ObjectRetrievalFailureException(theClass, id, e
74 .getMessage(), e);
75 }
76 }
77
78 /***
79 * Find objects by property value
80 *
81 * @param c
82 * class of the hibernated object
83 * @param propertyName
84 * name of the property
85 * @param value
86 * value to find
87 * @param firstElement
88 * the first result, numbered from 0
89 * @param maxElements
90 * the maximum number of results
91 * @return
92 */
93 protected List findByProperty(final Class c, final String propertyName,
94 final Object value, final int firstElement, final int maxElements) {
95
96 return getHibernateTemplate().executeFind(
97 getFindCallback(c, propertyName, value, firstElement,
98 maxElements));
99 }
100
101 /***
102 * Find objects by property value where the property contains the value
103 *
104 * @param c
105 * class of the hibernated object
106 * @param propertyName
107 * name of the property
108 * @param value
109 * value to find
110 * @param firstElement
111 * the first result, numbered from 0
112 * @param maxElements
113 * the maximum number of results
114 * @return
115 */
116 protected List findByPropertyContent(final Class c,
117 final String propertyName, final String value,
118 final int firstElement, final int maxElements) {
119
120 return getHibernateTemplate().executeFind(
121 getContentFindCallback(c, propertyName, value, firstElement,
122 maxElements));
123 }
124
125 /***
126 * Get a new HibernateCallback for finding objects by the value of a
127 * property, paginating the results
128 *
129 * @param c
130 * class of the hibernated object
131 * @param propertyName
132 * name of the property
133 * @param value
134 * value of the property
135 * @param firstElement
136 * the first result, numbered from 0
137 * @param maxElements
138 * the maximum number of results
139 * @return
140 */
141 private static HibernateCallback getFindCallback(final Class c,
142 final String propertyName, final Object value,
143 final int firstElement, final int maxElements) {
144
145 return new HibernateCallback() {
146
147 public Object doInHibernate(Session session)
148 throws HibernateException {
149 return session.createCriteria(c).add(
150 Expression.like(propertyName, value)).setFirstResult(
151 firstElement).setMaxResults(maxElements).list();
152 }
153 };
154 }
155
156 /***
157 * Get a new HibernateCallback for finding objects by the content of a
158 * property, paginating the results
159 *
160 * @param c
161 * class of the hibernated object
162 * @param propertyName
163 * name of the property
164 * @param value
165 * property must contain this value
166 * @param firstElement
167 * the first result, numbered from 0
168 * @param maxElements
169 * the maximum number of results
170 * @return
171 */
172 private static HibernateCallback getContentFindCallback(final Class c,
173 final String propertyName, final String value,
174 final int firstElement, final int maxElements) {
175
176 return new HibernateCallback() {
177
178 public Object doInHibernate(Session session)
179 throws HibernateException {
180 return session.createCriteria(c).add(
181 Expression.ilike(propertyName, value,
182 MatchMode.ANYWHERE)).setFirstResult(
183 firstElement).setMaxResults(maxElements).list();
184 }
185 };
186 }
187
188 /***
189 * Apply a filter to a persistent collection. A filter is a Hibernate query
190 * that may refer to this, the collection element. Filters allow efficient
191 * access to very large lazy collections. (Executing the filter does not
192 * initialize the collection.)
193 *
194 * @param collection
195 * a persistent collection to filter
196 * @param filter
197 * a filter query string
198 * @return Collection the resulting collection
199 *
200 * @see Session.filter()
201 */
202 protected Collection filter(Collection collection, String filter) {
203 return (Collection) getHibernateTemplate().execute(
204 getFilterCallback(collection, filter));
205
206 }
207
208 /***
209 * <p>
210 * Get a new HibernateCallback for filtering collections.
211 * </p>
212 * <p>
213 * Apply a filter to a persistent collection. A filter is a Hibernate query
214 * that may refer to this, the collection element. Filters allow efficient
215 * access to very large lazy collections. (Executing the filter does not
216 * initialize the collection.)
217 * </p>
218 *
219 * @param collection
220 * a persistent collection to filter
221 * @param filter
222 * a filter query string
223 * @return Collection the resulting collection
224 */
225 private static HibernateCallback getFilterCallback(
226 final Collection collection, final String filter) {
227
228 return new HibernateCallback() {
229
230 public Object doInHibernate(Session session)
231 throws HibernateException {
232 return session.filter(collection, filter);
233 }
234 };
235 }
236
237 }