1. /*
  2. * Copyright 2002-2004 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.orm.hibernate;
  17. import java.io.Serializable;
  18. import java.util.Collection;
  19. import java.util.List;
  20. import net.sf.hibernate.LockMode;
  21. import net.sf.hibernate.type.Type;
  22. import org.springframework.dao.DataAccessException;
  23. /**
  24. * Interface that specifies a basic set of Hibernate operations.
  25. * Implemented by HibernateTemplate. Not often used, but a useful option
  26. * to enhance testability, as it can easily be mocked or stubbed.
  27. *
  28. * <p>Provides HibernateTemplate's convenience methods that mirror
  29. * various Session methods.
  30. *
  31. * @author Juergen Hoeller
  32. * @since 05.02.2004
  33. * @see HibernateTemplate
  34. * @see net.sf.hibernate.Session
  35. */
  36. public interface HibernateOperations {
  37. /**
  38. * Execute the action specified by the given action object within a session.
  39. * Application exceptions thrown by the action object get propagated to the
  40. * caller (can only be unchecked). Hibernate exceptions are transformed into
  41. * appropriate DAO ones. Allows for returning a result object, i.e. a domain
  42. * object or a collection of domain objects.
  43. * <p>Note: Callback code is not supposed to handle transactions itself!
  44. * Use an appropriate transaction manager like HibernateTransactionManager.
  45. * Generally, callback code must not touch any Session lifecycle methods,
  46. * like close, disconnect, or reconnect, to let the template do its work.
  47. * @param action callback object that specifies the Hibernate action
  48. * @return a result object returned by the action, or null
  49. * @throws DataAccessException in case of Hibernate errors
  50. * @see HibernateTransactionManager
  51. * @see org.springframework.dao
  52. * @see org.springframework.transaction
  53. */
  54. Object execute(HibernateCallback action) throws DataAccessException;
  55. /**
  56. * Execute the specified action assuming that the result object is a List.
  57. * <p>This is a convenience method for executing Hibernate find calls
  58. * within an action.
  59. * @param action action object that specifies the Hibernate action
  60. * @return a result object returned by the action, or null
  61. * @throws DataAccessException in case of Hibernate errors
  62. */
  63. List executeFind(HibernateCallback action) throws DataAccessException;
  64. //-------------------------------------------------------------------------
  65. // Convenience methods for load, save, update, delete
  66. //-------------------------------------------------------------------------
  67. /**
  68. * Return the persistent instance of the given entity class
  69. * with the given identifier, or null if not found.
  70. * @param entityClass a persistent class
  71. * @param id an identifier of the persistent instance
  72. * @return the persistent instance, or null if not found
  73. * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
  74. * @see net.sf.hibernate.Session#get(Class, java.io.Serializable)
  75. */
  76. Object get(Class entityClass, Serializable id) throws DataAccessException;
  77. /**
  78. * Return the persistent instance of the given entity class
  79. * with the given identifier, or null if not found.
  80. * Obtains the specified lock mode if the instance exists.
  81. * @param entityClass a persistent class
  82. * @param id an identifier of the persistent instance
  83. * @return the persistent instance, or null if not found
  84. * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
  85. * @see net.sf.hibernate.Session#get(Class, java.io.Serializable, net.sf.hibernate.LockMode)
  86. */
  87. Object get(Class entityClass, Serializable id, LockMode lockMode)
  88. throws DataAccessException;
  89. /**
  90. * Return the persistent instance of the given entity class
  91. * with the given identifier, throwing an exception if not found.
  92. * @param entityClass a persistent class
  93. * @param id an identifier of the persistent instance
  94. * @return the persistent instance
  95. * @throws HibernateObjectRetrievalFailureException if the instance could not be found
  96. * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
  97. * @see net.sf.hibernate.Session#load(Class, java.io.Serializable)
  98. */
  99. Object load(Class entityClass, Serializable id) throws DataAccessException;
  100. /**
  101. * Return the persistent instance of the given entity class
  102. * with the given identifier, throwing an exception if not found.
  103. * Obtains the specified lock mode if the instance exists.
  104. * @param entityClass a persistent class
  105. * @param id an identifier of the persistent instance
  106. * @return the persistent instance
  107. * @throws HibernateObjectRetrievalFailureException if the instance could not be found
  108. * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
  109. * @see net.sf.hibernate.Session#load(Class, java.io.Serializable)
  110. */
  111. Object load(Class entityClass, Serializable id, LockMode lockMode)
  112. throws DataAccessException;
  113. /**
  114. * Return all persistent instances of the given entity class.
  115. * Note: Use queries or criteria for retrieving a specific subset.
  116. * @param entityClass a persistent class
  117. * @return a List containing 0 or more persistent instances
  118. * @throws DataAccessException if there is a Hibernate error
  119. * @see net.sf.hibernate.Session#createCriteria
  120. */
  121. List loadAll(Class entityClass) throws DataAccessException;
  122. /**
  123. * Remove the given object from the Session cache.
  124. * @param entity the persistent instance to lock
  125. * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
  126. * @see net.sf.hibernate.Session#evict(Object)
  127. */
  128. void evict(Object entity) throws DataAccessException;
  129. /**
  130. * Obtain the specified lock level upon the given object, implicitly
  131. * checking whether the corresponding database entry still exists
  132. * (throwing an OptimisticLockingFailureException if not found).
  133. * @param entity the persistent instance to lock
  134. * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
  135. * @see HibernateOptimisticLockingFailureException
  136. * @see net.sf.hibernate.Session#lock(Object, net.sf.hibernate.LockMode)
  137. */
  138. void lock(Object entity, LockMode lockMode) throws DataAccessException;
  139. /**
  140. * Save the given persistent instance.
  141. * @param entity the persistent instance to save
  142. * @return the generated identifier
  143. * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
  144. * @see net.sf.hibernate.Session#save(Object)
  145. */
  146. Serializable save(Object entity) throws DataAccessException;
  147. /**
  148. * Save the given persistent instance with the given identifier.
  149. * @param entity the persistent instance to save
  150. * @param id the identifier to assign
  151. * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
  152. * @see net.sf.hibernate.Session#save(Object, java.io.Serializable)
  153. */
  154. void save(Object entity, Serializable id) throws DataAccessException;
  155. /**
  156. * Save respectively update the given persistent instance,
  157. * according to its ID (matching the configured "unsaved-value"?).
  158. * @param entity the persistent instance to save respectively update
  159. * (to be associated with the Hibernate Session)
  160. * @throws DataAccessException in case of Hibernate errors
  161. * @see net.sf.hibernate.Session#saveOrUpdate(Object)
  162. */
  163. void saveOrUpdate(Object entity) throws DataAccessException;
  164. /**
  165. * Save respectively update the contents of given persistent object,
  166. * according to its ID (matching the configured "unsaved-value"?).
  167. * Will copy the contained fields to an already loaded instance
  168. * with the same ID, if appropriate.
  169. * @param entity the persistent object to save respectively update
  170. * (<i>not</i> necessarily to be associated with the Hibernate Session)
  171. * @return the actually associated persistent object
  172. * (either an already loaded instance with the same ID, or the given object)
  173. * @throws DataAccessException in case of Hibernate errors
  174. * @see net.sf.hibernate.Session#saveOrUpdateCopy(Object)
  175. */
  176. Object saveOrUpdateCopy(Object entity) throws DataAccessException;
  177. /**
  178. * Update the given persistent instance.
  179. * @param entity the persistent instance to update
  180. * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
  181. * @see net.sf.hibernate.Session#update(Object)
  182. */
  183. void update(Object entity) throws DataAccessException;
  184. /**
  185. * Update the given persistent instance.
  186. * Obtains the specified lock mode if the instance exists, implicitly
  187. * checking whether the corresponding database entry still exists
  188. * (throwing an OptimisticLockingFailureException if not found).
  189. * @param entity the persistent instance to update
  190. * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
  191. * @see HibernateOptimisticLockingFailureException
  192. * @see net.sf.hibernate.Session#update(Object)
  193. */
  194. void update(Object entity, LockMode lockMode) throws DataAccessException;
  195. /**
  196. * Delete the given persistent instance.
  197. * @param entity the persistent instance to delete
  198. * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
  199. * @see net.sf.hibernate.Session#delete(Object)
  200. */
  201. void delete(Object entity) throws DataAccessException;
  202. /**
  203. * Delete the given persistent instance.
  204. * Obtains the specified lock mode if the instance exists, implicitly
  205. * checking whether the corresponding database entry still exists
  206. * (throwing an OptimisticLockingFailureException if not found).
  207. * @param entity the persistent instance to delete
  208. * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
  209. * @see HibernateOptimisticLockingFailureException
  210. * @see net.sf.hibernate.Session#delete(Object)
  211. */
  212. void delete(Object entity, LockMode lockMode) throws DataAccessException;
  213. /**
  214. * Delete all given persistent instances.
  215. * This can be combined with any of the find methods to delete by query
  216. * in two lines of code, similar to Session's delete by query methods.
  217. * @param entities the persistent instances to delete
  218. * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
  219. * @see net.sf.hibernate.Session#delete(String)
  220. */
  221. void deleteAll(Collection entities) throws DataAccessException;
  222. //-------------------------------------------------------------------------
  223. // Convenience finder methods
  224. //-------------------------------------------------------------------------
  225. /**
  226. * Execute a query for persistent instances.
  227. * @param queryString a query expressed in Hibernate's query language
  228. * @return a List containing 0 or more persistent instances
  229. * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
  230. * @see net.sf.hibernate.Session#find(String)
  231. * @see net.sf.hibernate.Session#createQuery
  232. */
  233. List find(String queryString) throws DataAccessException;
  234. /**
  235. * Execute a query for persistent instances, binding
  236. * one value to a "?" parameter in the query string.
  237. * @param queryString a query expressed in Hibernate's query language
  238. * @param value the value of the parameter
  239. * @return a List containing 0 or more persistent instances
  240. * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
  241. * @see net.sf.hibernate.Session#find(String)
  242. * @see net.sf.hibernate.Session#createQuery
  243. */
  244. List find(String queryString, Object value) throws DataAccessException;
  245. /**
  246. * Execute a query for persistent instances, binding one value
  247. * to a "?" parameter of the given type in the query string.
  248. * @param queryString a query expressed in Hibernate's query language
  249. * @param value the value of the parameter
  250. * @param type Hibernate type of the parameter
  251. * @return a List containing 0 or more persistent instances
  252. * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
  253. * @see net.sf.hibernate.Session#find(String)
  254. * @see net.sf.hibernate.Session#createQuery
  255. */
  256. List find(String queryString, Object value, Type type) throws DataAccessException;
  257. /**
  258. * Execute a query for persistent instances, binding a
  259. * number of values to "?" parameters in the query string.
  260. * @param queryString a query expressed in Hibernate's query language
  261. * @param values the values of the parameters
  262. * @return a List containing 0 or more persistent instances
  263. * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
  264. * @see net.sf.hibernate.Session#find(String)
  265. * @see net.sf.hibernate.Session#createQuery
  266. */
  267. List find(String queryString, Object[] values) throws DataAccessException;
  268. /**
  269. * Execute a query for persistent instances, binding a number of
  270. * values to "?" parameters of the given types in the query string.
  271. * @param queryString a query expressed in Hibernate's query language
  272. * @param values the values of the parameters
  273. * @param types Hibernate types of the parameters
  274. * @return a List containing 0 or more persistent instances
  275. * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
  276. * @see net.sf.hibernate.Session#find(String)
  277. * @see net.sf.hibernate.Session#createQuery
  278. */
  279. List find(String queryString, Object[] values, Type[] types) throws DataAccessException;
  280. /**
  281. * Execute a query for persistent instances, binding the properties
  282. * of the given bean to <i>named</i> parameters in the query string.
  283. * @param queryString a query expressed in Hibernate's query language
  284. * @param valueBean the values of the parameters
  285. * @return a List containing 0 or more persistent instances
  286. * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
  287. * @see net.sf.hibernate.Session#find(String)
  288. * @see net.sf.hibernate.Session#createQuery
  289. * @see net.sf.hibernate.Query#setProperties
  290. */
  291. List findByValueBean(String queryString, Object valueBean) throws DataAccessException;
  292. /**
  293. * Execute a named query for persistent instances.
  294. * A named query is defined in a Hibernate mapping file.
  295. * @param queryName the name of a Hibernate query in a mapping file
  296. * @return a List containing 0 or more persistent instances
  297. * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
  298. * @see net.sf.hibernate.Session#find(String)
  299. * @see net.sf.hibernate.Session#getNamedQuery(String)
  300. */
  301. List findByNamedQuery(String queryName) throws DataAccessException;
  302. /**
  303. * Execute a named query for persistent instances, binding
  304. * one value to a "?" parameter in the query string.
  305. * A named query is defined in a Hibernate mapping file.
  306. * @param queryName the name of a Hibernate query in a mapping file
  307. * @return a List containing 0 or more persistent instances
  308. * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
  309. * @see net.sf.hibernate.Session#find(String)
  310. * @see net.sf.hibernate.Session#getNamedQuery(String)
  311. */
  312. List findByNamedQuery(String queryName, Object value) throws DataAccessException;
  313. /**
  314. * Execute a named query for persistent instances, binding
  315. * one value to a "?" parameter in the query string.
  316. * A named query is defined in a Hibernate mapping file.
  317. * @param queryName the name of a Hibernate query in a mapping file
  318. * @param type Hibernate type of the parameter
  319. * @return a List containing 0 or more persistent instances
  320. * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
  321. * @see net.sf.hibernate.Session#find(String)
  322. * @see net.sf.hibernate.Session#getNamedQuery(String)
  323. */
  324. List findByNamedQuery(String queryName, Object value, Type type) throws DataAccessException;
  325. /**
  326. * Execute a named query for persistent instances, binding a
  327. * number of values to "?" parameters in the query string.
  328. * A named query is defined in a Hibernate mapping file.
  329. * @param queryName the name of a Hibernate query in a mapping file
  330. * @param values the values of the parameters
  331. * @return a List containing 0 or more persistent instances
  332. * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
  333. * @see net.sf.hibernate.Session#find(String)
  334. * @see net.sf.hibernate.Session#getNamedQuery(String)
  335. */
  336. List findByNamedQuery(String queryName, Object[] values) throws DataAccessException;
  337. /**
  338. * Execute a named query for persistent instances, binding a
  339. * number of values to "?" parameters in the query string.
  340. * A named query is defined in a Hibernate mapping file.
  341. * @param queryName the name of a Hibernate query in a mapping file
  342. * @param values the values of the parameters
  343. * @param types Hibernate types of the parameters
  344. * @return a List containing 0 or more persistent instances
  345. * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
  346. * @see net.sf.hibernate.Session#find(String)
  347. * @see net.sf.hibernate.Session#getNamedQuery(String)
  348. */
  349. List findByNamedQuery(String queryName, Object[] values, Type[] types)
  350. throws DataAccessException;
  351. /**
  352. * Execute a named query for persistent instances, binding
  353. * one value to a ":" named parameter in the query string.
  354. * A named query is defined in a Hibernate mapping file.
  355. * @param queryName the name of a Hibernate query in a mapping file
  356. * @param paramName the name of parameter
  357. * @param value the value of the parameter
  358. * @return a List containing 0 or more persistent instances
  359. * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
  360. * @see net.sf.hibernate.Session#find(String)
  361. * @see net.sf.hibernate.Session#getNamedQuery(String)
  362. */
  363. List findByNamedQuery(String queryName, String paramName, Object value)
  364. throws DataAccessException;
  365. /**
  366. * Execute a named query for persistent instances, binding
  367. * one value to a ":" named parameter in the query string.
  368. * A named query is defined in a Hibernate mapping file.
  369. * @param queryName the name of a Hibernate query in a mapping file
  370. * @param paramName the name of the parameter
  371. * @param value the value of the parameter
  372. * @param type Hibernate type of the parameter
  373. * @return a List containing 0 or more persistent instances
  374. * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
  375. * @see net.sf.hibernate.Session#find(String)
  376. * @see net.sf.hibernate.Session#getNamedQuery(String)
  377. */
  378. List findByNamedQuery(String queryName, String paramName, Object value, Type type)
  379. throws DataAccessException;
  380. /**
  381. * Execute a named query for persistent instances, binding a
  382. * number of values to ":" named parameters in the query string.
  383. * A named query is defined in a Hibernate mapping file.
  384. * @param queryName the name of a Hibernate query in a mapping file
  385. * @param paramNames the names of the parameters
  386. * @param values the values of the parameters
  387. * @return a List containing 0 or more persistent instances
  388. * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
  389. * @see net.sf.hibernate.Session#find(String)
  390. * @see net.sf.hibernate.Session#getNamedQuery(String)
  391. */
  392. List findByNamedQuery(String queryName, String[] paramNames, Object[] values)
  393. throws DataAccessException;
  394. /**
  395. * Execute a named query for persistent instances, binding a
  396. * number of values to ":" named parameters in the query string.
  397. * A named query is defined in a Hibernate mapping file.
  398. * @param queryName the name of a Hibernate query in a mapping file
  399. * @param paramNames the names of the parameters
  400. * @param values the values of the parameters
  401. * @param types Hibernate types of the parameters
  402. * @return a List containing 0 or more persistent instances
  403. * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
  404. * @see net.sf.hibernate.Session#find(String)
  405. * @see net.sf.hibernate.Session#getNamedQuery(String)
  406. */
  407. List findByNamedQuery(String queryName, String[] paramNames, Object[] values, Type[] types)
  408. throws DataAccessException;
  409. /**
  410. * Execute a named query for persistent instances, binding the properties
  411. * of the given bean to ":" named parameters in the query string.
  412. * A named query is defined in a Hibernate mapping file.
  413. * @param queryName the name of a Hibernate query in a mapping file
  414. * @param valueBean the values of the parameters
  415. * @return a List containing 0 or more persistent instances
  416. * @throws org.springframework.dao.DataAccessException in case of Hibernate errors
  417. * @see net.sf.hibernate.Session#find(String)
  418. * @see net.sf.hibernate.Session#getNamedQuery(String)
  419. * @see net.sf.hibernate.Query#setProperties
  420. */
  421. List findByNamedQueryAndValueBean(String queryName, Object valueBean)
  422. throws DataAccessException;
  423. }