Class HibernateTemplate

  • All Implemented Interfaces:
    Aware, BeanFactoryAware, InitializingBean, HibernateOperations

    @Deprecated
    public class HibernateTemplate
    extends HibernateAccessor
    implements HibernateOperations
    Deprecated.
    as of Spring 4.3, in favor of Hibernate 4.x/5.x
    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 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.

    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 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 "allowCreate" to "false".

    LocalSessionFactoryBean is the preferred way of obtaining a reference to 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.

    Since:
    1.2
    Author:
    Juergen Hoeller
    See Also:
    HibernateAccessor.setSessionFactory(org.hibernate.SessionFactory), HibernateCallback, Session, LocalSessionFactoryBean, HibernateTransactionManager, JtaTransactionManager, OpenSessionInViewFilter, OpenSessionInViewInterceptor