Helper class that simplifies Hibernate data access code, and converts
checked HibernateExceptions into unchecked DataAccessExceptions,
following the 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 code
implementing the HibernateCallback interface. It provides Hibernate Session
handling 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
Hibernate3 Session API (through SessionFactory.getCurrentSession()).
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 HibernateTransactionManager is used for transaction
demarcation 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.
LocalSessionFactoryBean is the preferred way of obtaining a reference
to a specific Hibernate SessionFactory, at least in a non-EJB environment.
Alternatively, use a JndiObjectFactoryBean to fetch a SessionFactory
from JNDI (possibly set up via a JCA Connector).
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.
org.springframework.daoexception 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.daoexceptions.The central method is
execute, supporting Hibernate code implementing the HibernateCallback interface. It provides Hibernate Session handling 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 Hibernate3 Session API (through
SessionFactory.getCurrentSession()). 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 HibernateTransactionManager is used for transaction demarcation 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.
LocalSessionFactoryBean is the preferred way of obtaining a reference to a specific Hibernate SessionFactory, at least in a non-EJB environment. Alternatively, use a JndiObjectFactoryBean to fetch a SessionFactory from JNDI (possibly set up via a JCA Connector).
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.