public abstract class

HibernateDaoSupport

extends DaoSupport
java.lang.Object
   ↳ org.springframework.dao.support.DaoSupport
     ↳ org.springframework.orm.hibernate3.support.HibernateDaoSupport

Class Overview

Convenient super class for Hibernate-based data access objects.

Requires a org.hibernate.SessionFactory to be set, providing a HibernateTemplate based on it to subclasses through the getHibernateTemplate() method. Can alternatively be initialized directly with a HibernateTemplate, in order to reuse the latter's settings such as the SessionFactory, exception translator, flush mode, etc.

This base class is mainly intended for HibernateTemplate usage but can also be used when working with a Hibernate Session directly, for example when relying on transactional Sessions. Convenience getSession() and releaseSession(Session) methods are provided for that usage style.

This class will create its own HibernateTemplate instance if a SessionFactory is passed in. The "allowCreate" flag on that HibernateTemplate will be "true" by default. A custom HibernateTemplate instance can be used through overriding createHibernateTemplate(SessionFactory).

Summary

[Expand]
Inherited Fields
From class org.springframework.dao.support.DaoSupport
Public Constructors
HibernateDaoSupport()
Public Methods
final HibernateTemplate getHibernateTemplate()
Return the HibernateTemplate for this DAO, pre-initialized with the SessionFactory or set explicitly.
final SessionFactory getSessionFactory()
Return the Hibernate SessionFactory used by this DAO.
final void setHibernateTemplate(HibernateTemplate hibernateTemplate)
Set the HibernateTemplate for this DAO explicitly, as an alternative to specifying a SessionFactory.
final void setSessionFactory(SessionFactory sessionFactory)
Set the Hibernate SessionFactory to be used by this DAO.
Protected Methods
final void checkDaoConfig()
Abstract subclasses must override this to check their configuration.
final DataAccessException convertHibernateAccessException(HibernateException ex)
Convert the given HibernateException to an appropriate exception from the org.springframework.dao hierarchy.
HibernateTemplate createHibernateTemplate(SessionFactory sessionFactory)
Create a HibernateTemplate for the given SessionFactory.
final Session getSession()
Obtain a Hibernate Session, either from the current transaction or a new one.
final Session getSession(boolean allowCreate)
Obtain a Hibernate Session, either from the current transaction or a new one.
final void releaseSession(Session session)
Close the given Hibernate Session, created via this DAO's SessionFactory, if it isn't bound to the thread (i.e.
[Expand]
Inherited Methods
From class org.springframework.dao.support.DaoSupport
From class java.lang.Object
From interface org.springframework.beans.factory.InitializingBean

Public Constructors

public HibernateDaoSupport ()

Public Methods

public final HibernateTemplate getHibernateTemplate ()

Return the HibernateTemplate for this DAO, pre-initialized with the SessionFactory or set explicitly.

Note: The returned HibernateTemplate is a shared instance. You may introspect its configuration, but not modify the configuration (other than from within an initDao() implementation). Consider creating a custom HibernateTemplate instance via new HibernateTemplate(getSessionFactory()), in which case you're allowed to customize the settings on the resulting instance.

public final SessionFactory getSessionFactory ()

Return the Hibernate SessionFactory used by this DAO.

public final void setHibernateTemplate (HibernateTemplate hibernateTemplate)

Set the HibernateTemplate for this DAO explicitly, as an alternative to specifying a SessionFactory.

public final void setSessionFactory (SessionFactory sessionFactory)

Set the Hibernate SessionFactory to be used by this DAO. Will automatically create a HibernateTemplate for the given SessionFactory.

Protected Methods

protected final void checkDaoConfig ()

Abstract subclasses must override this to check their configuration.

Implementors should be marked as final

protected final DataAccessException convertHibernateAccessException (HibernateException ex)

Convert the given HibernateException to an appropriate exception from the org.springframework.dao hierarchy. Will automatically detect wrapped SQLExceptions and convert them accordingly.

Delegates to the convertHibernateAccessException(HibernateException) method of this DAO's HibernateTemplate.

Typically used in plain Hibernate code, in combination with getSession() and releaseSession(Session).

Parameters
ex HibernateException that occurred
Returns
  • the corresponding DataAccessException instance

protected HibernateTemplate createHibernateTemplate (SessionFactory sessionFactory)

Create a HibernateTemplate for the given SessionFactory. Only invoked if populating the DAO with a SessionFactory reference!

Can be overridden in subclasses to provide a HibernateTemplate instance with different configuration, or a custom HibernateTemplate subclass.

Parameters
sessionFactory the Hibernate SessionFactory to create a HibernateTemplate for
Returns
  • the new HibernateTemplate instance

protected final Session getSession ()

Obtain a Hibernate Session, either from the current transaction or a new one. The latter is only allowed if the "allowCreate" setting of this bean's HibernateTemplate is "true".

Note that this is not meant to be invoked from HibernateTemplate code but rather just in plain Hibernate code. Either rely on a thread-bound Session or use it in combination with releaseSession(Session).

In general, it is recommended to use HibernateTemplate, either with the provided convenience operations or with a custom HibernateCallback that provides you with a Session to work on. HibernateTemplate will care for all resource management and for proper exception conversion.

Returns
  • the Hibernate Session
Throws
DataAccessResourceFailureException if the Session couldn't be created
IllegalStateException if no thread-bound Session found and allowCreate=false

protected final Session getSession (boolean allowCreate)

Obtain a Hibernate Session, either from the current transaction or a new one. The latter is only allowed if "allowCreate" is true.

Note that this is not meant to be invoked from HibernateTemplate code but rather just in plain Hibernate code. Either rely on a thread-bound Session or use it in combination with releaseSession(Session).

In general, it is recommended to use HibernateTemplate, either with the provided convenience operations or with a custom HibernateCallback that provides you with a Session to work on. HibernateTemplate will care for all resource management and for proper exception conversion.

Parameters
allowCreate if a non-transactional Session should be created when no transactional Session can be found for the current thread
Returns
  • the Hibernate Session
Throws
DataAccessResourceFailureException if the Session couldn't be created
IllegalStateException if no thread-bound Session found and allowCreate=false

protected final void releaseSession (Session session)

Close the given Hibernate Session, created via this DAO's SessionFactory, if it isn't bound to the thread (i.e. isn't a transactional Session).

Typically used in plain Hibernate code, in combination with getSession() and convertHibernateAccessException(HibernateException).

Parameters
session the Session to close