Helper class that simplifies Hibernate data access code. Automatically converts HibernateExceptions into DataAccessExceptions, following the
org.springframework.dao
exception hierarchy.
The central method is execute
, supporting Hibernate access code implementing the {@link HibernateCallback} interface. It provides Hibernate Sessionhandling such that neither the HibernateCallback implementation nor the calling code needs to explicitly care about retrieving/closing Hibernate Sessions, or handling Session lifecycle exceptions. For typical single step actions, there are various convenience methods (find, load, saveOrUpdate, delete).
Can be used within a service implementation via direct instantiation with a SessionFactory reference, or get prepared in an application context and given to services as bean reference. Note: The SessionFactory should always be configured as bean in the application context, in the first case given to the service directly, in the second case to the prepared template.
NOTE: As of Hibernate 3.0.1, transactional Hibernate access code can also be coded in plain Hibernate style. Hence, for newly started projects, consider adopting the standard Hibernate3 style of coding data access objects instead, based on {@link org.hibernate.SessionFactory#getCurrentSession()}.
This class can be considered as direct alternative to working with the raw Hibernate3 Session API (through SessionFactory.getCurrentSession()
). The major advantage is its automatic conversion to DataAccessExceptions as well as its capability to fall back to 'auto-commit' style behavior when used outside of transactions. Note that HibernateTemplate will perform its own Session management, not participating in a custom Hibernate CurrentSessionContext unless you explicitly switch {@link #setAllowCreate "allowCreate"} to "false".
{@link LocalSessionFactoryBean} is the preferred way of obtaining a referenceto a specific Hibernate SessionFactory, at least in a non-EJB environment. The Spring application context will manage its lifecycle, initializing and shutting down the factory as part of the application.
Note that operations that return an Iterator (i.e. iterate
) are supposed to be used within Spring-driven or JTA-driven transactions (with HibernateTransactionManager, JtaTransactionManager, or EJB CMT). Else, the Iterator won't be able to read results from its ResultSet anymore, as the underlying Hibernate Session will already have been closed.
Lazy loading will also just work with an open Hibernate Session, either within a transaction or within OpenSessionInViewFilter/Interceptor. Furthermore, some operations just make sense within transactions, for example: contains
, evict
, lock
, flush
, clear
.
@author Juergen Hoeller
@since 1.2
@see #setSessionFactory
@see HibernateCallback
@see org.hibernate.Session
@see LocalSessionFactoryBean
@see HibernateTransactionManager
@see org.springframework.transaction.jta.JtaTransactionManager
@see org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
@see org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor