org.springframework.dao
exception hierarchy. Uses the same SQLExceptionTranslator mechanism as JdbcTemplate. Typically used to implement data access or business logic services that use Hibernate within their implementation but are Hibernate-agnostic in their interface. The latter or code calling the latter only have to deal with domain objects, query objects, and org.springframework.dao
exceptions.
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.
This class can be considered as direct alternative to working with the raw Hibernate Session API (through SessionFactoryUtils.getSession()
). The major advantage is its automatic conversion to DataAccessExceptions, the major disadvantage that no checked application exceptions can get thrown from within data access code. Corresponding checks and the actual throwing of such exceptions can often be deferred to after callback execution, though.
Note that even if {@link HibernateTransactionManager} is used for transactiondemarcation in higher-level services, all those services above the data access layer don't need to be Hibernate-aware. Setting such a special PlatformTransactionManager is a configuration issue: For example, switching to JTA is just a matter of Spring configuration (use JtaTransactionManager instead) that does not affect application code.
{@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
.
Note: Spring's Hibernate support in this package requires Hibernate 2.1. Dedicated Hibernate3 support can be found in a separate package: org.springframework.orm.hibernate3
.
@author Juergen Hoeller
@since 02.05.2003
@see #setSessionFactory
@see #setJdbcExceptionTranslator
@see HibernateCallback
@see net.sf.hibernate.Session
@see HibernateInterceptor
@see LocalSessionFactoryBean
@see org.springframework.jndi.JndiObjectFactoryBean
@see org.springframework.jdbc.support.SQLExceptionTranslator
@see HibernateTransactionManager
@see org.springframework.transaction.jta.JtaTransactionManager
@see org.springframework.orm.hibernate.support.OpenSessionInViewFilter
@see org.springframework.orm.hibernate.support.OpenSessionInViewInterceptor
|
|
|
|