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.sql.SQLException;
  18. import net.sf.hibernate.HibernateException;
  19. import net.sf.hibernate.Session;
  20. /**
  21. * Callback interface for Hibernate code. To be used with HibernateTemplate's execute
  22. * method, assumably often as anonymous classes within a method implementation.
  23. * The typical implementation will call Session.load/find/save/update to perform
  24. * some operations on persistent objects. It can also perform direct JDBC operations
  25. * via Hibernate's Session.connection() method, returning the active JDBC connection.
  26. *
  27. * <p>Note that Hibernate works on unmodified plain Java objects, performing dirty
  28. * detection via copies made at load time. Returned objects can thus be used outside
  29. * of an active Hibernate Session without any hassle, e.g. for display in a web GUI.
  30. * Reassociating such instances with a new Session, e.g. for updates when coming
  31. * back from the GUI, is straightforward, as the instance has kept its identity.
  32. * You should care to reassociate them as early as possible though, to avoid having
  33. * already loaded a version from the database in the same Session.
  34. *
  35. * @author Juergen Hoeller
  36. * @since 02.05.2003
  37. * @see HibernateTemplate
  38. */
  39. public interface HibernateCallback {
  40. /**
  41. * Gets called by HibernateTemplate.execute with an active Hibernate Session.
  42. * Does not need to care about activating or closing the Session,
  43. * or handling transactions.
  44. *
  45. * <p>If called without a thread-bound Hibernate transaction (initiated
  46. * by HibernateTransactionManager), the code will simply get executed on the
  47. * underlying JDBC connection with its transactional semantics. If Hibernate
  48. * is configured to use a JTA-aware DataSource, the JDBC connection and thus
  49. * the callback code will be transactional if a JTA transaction is active.
  50. *
  51. * <p>Allows for returning a result object created within the callback, i.e.
  52. * a domain object or a collection of domain objects. Note that there's
  53. * special support for single step actions: see HibernateTemplate.find etc.
  54. * A thrown RuntimeException is treated as application exception, it gets
  55. * propagated to the caller of the template.
  56. *
  57. * @param session active Hibernate session
  58. * @return a result object, or null if none
  59. * @throws HibernateException in case of Hibernate errors
  60. * @throws SQLException in case of errors on direct JDBC access
  61. * @see HibernateTemplate#execute
  62. * @see HibernateTransactionManager
  63. */
  64. Object doInHibernate(Session session) throws HibernateException, SQLException;
  65. }