org.springframework.orm.hibernate.support
Class HibernateDaoSupport

java.lang.Object
  extended byorg.springframework.orm.hibernate.support.HibernateDaoSupport
All Implemented Interfaces:
InitializingBean

public abstract class HibernateDaoSupport
extends Object
implements InitializingBean

Convenient super class for Hibernate data access objects.

Requires a SessionFactory to be set, providing a HibernateTemplate based on it to subclasses. Can alternatively be initialized directly via a HibernateTemplate, to reuse the latter's settings like SessionFactory, exception translator, flush mode, etc.

This base class is mainly intended for HibernateTemplate usage but can also be used when working with SessionFactoryUtils directly, e.g. in combination with HibernateInterceptor-managed Sessions. Convenience getSession and closeSessionIfNecessary methods are provided for that usage style.

This class will create its own HibernateTemplate if only 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.

Since:
28.07.2003
Author:
Juergen Hoeller
See Also:
setSessionFactory(net.sf.hibernate.SessionFactory), setHibernateTemplate(org.springframework.orm.hibernate.HibernateTemplate), createHibernateTemplate(net.sf.hibernate.SessionFactory), getSession(), closeSessionIfNecessary(net.sf.hibernate.Session), HibernateTemplate, HibernateInterceptor

Field Summary
protected  Log logger
           
 
Constructor Summary
HibernateDaoSupport()
           
 
Method Summary
 void afterPropertiesSet()
          Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).
protected  void closeSessionIfNecessary(Session session)
          Close the given Hibernate Session if necessary, created via this bean's SessionFactory, if it isn't bound to the thread.
protected  DataAccessException convertHibernateAccessException(HibernateException ex)
          Convert the given HibernateException to an appropriate exception from the org.springframework.dao hierarchy.
protected  HibernateTemplate createHibernateTemplate(SessionFactory sessionFactory)
          Create a HibernateTemplate for the given SessionFactory.
 HibernateTemplate getHibernateTemplate()
          Return the HibernateTemplate for this DAO, pre-initialized with the SessionFactory or set explicitly.
protected  Session getSession()
          Get a Hibernate Session, either from the current transaction or a new one.
protected  Session getSession(boolean allowCreate)
          Get a Hibernate Session, either from the current transaction or a new one.
 SessionFactory getSessionFactory()
          Return the Hibernate SessionFactory used by this DAO.
protected  void initDao()
          Subclasses can override this for custom initialization behavior.
 void setHibernateTemplate(HibernateTemplate hibernateTemplate)
          Set the HibernateTemplate for this DAO explicitly, as an alternative to specifying a SessionFactory.
 void setSessionFactory(SessionFactory sessionFactory)
          Set the Hibernate SessionFactory to be used by this DAO.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

logger

protected final Log logger
Constructor Detail

HibernateDaoSupport

public HibernateDaoSupport()
Method Detail

setSessionFactory

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.

See Also:
createHibernateTemplate(net.sf.hibernate.SessionFactory), setHibernateTemplate(org.springframework.orm.hibernate.HibernateTemplate)

createHibernateTemplate

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
See Also:
setSessionFactory(net.sf.hibernate.SessionFactory)

getSessionFactory

public final SessionFactory getSessionFactory()
Return the Hibernate SessionFactory used by this DAO.


setHibernateTemplate

public final void setHibernateTemplate(HibernateTemplate hibernateTemplate)
Set the HibernateTemplate for this DAO explicitly, as an alternative to specifying a SessionFactory.

See Also:
setSessionFactory(net.sf.hibernate.SessionFactory)

getHibernateTemplate

public final HibernateTemplate getHibernateTemplate()
Return the HibernateTemplate for this DAO, pre-initialized with the SessionFactory or set explicitly.


afterPropertiesSet

public final void afterPropertiesSet()
                              throws Exception
Description copied from interface: InitializingBean
Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).

This method allows the bean instance to perform initialization only possible when all bean properties have been set and to throw an exception in the event of misconfiguration.

Specified by:
afterPropertiesSet in interface InitializingBean
Throws:
Exception - in the event of misconfiguration (such as failure to set an essential property) or if initialization fails.

initDao

protected void initDao()
                throws Exception
Subclasses can override this for custom initialization behavior. Gets called after population of this instance's bean properties.

Throws:
Exception - if initialization fails

getSession

protected final Session getSession()
                            throws DataAccessResourceFailureException,
                                   IllegalStateException
Get 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 (via HibernateInterceptor), or use it in combination with closeSessionIfNecessary.

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
See Also:
closeSessionIfNecessary(net.sf.hibernate.Session), SessionFactoryUtils.getSession(SessionFactory, boolean), HibernateInterceptor, HibernateTemplate, HibernateCallback

getSession

protected final Session getSession(boolean allowCreate)
                            throws DataAccessResourceFailureException,
                                   IllegalStateException
Get 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 (via HibernateInterceptor), or use it in combination with closeSessionIfNecessary.

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 new Session should be created if no thread-bound found
Returns:
the Hibernate Session
Throws:
DataAccessResourceFailureException - if the Session couldn't be created
IllegalStateException - if no thread-bound Session found and allowCreate false
See Also:
closeSessionIfNecessary(net.sf.hibernate.Session), SessionFactoryUtils.getSession(SessionFactory, boolean), HibernateInterceptor, HibernateTemplate, HibernateCallback

convertHibernateAccessException

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 method of this DAO's HibernateTemplate.

Typically used in plain Hibernate code, in combination with getSession and closeSessionIfNecessary.

Parameters:
ex - HibernateException that occured
Returns:
the corresponding DataAccessException instance
See Also:
setHibernateTemplate(org.springframework.orm.hibernate.HibernateTemplate), getSession(), closeSessionIfNecessary(net.sf.hibernate.Session), HibernateAccessor.convertHibernateAccessException(net.sf.hibernate.HibernateException)

closeSessionIfNecessary

protected final void closeSessionIfNecessary(Session session)
Close the given Hibernate Session if necessary, created via this bean's SessionFactory, if it isn't bound to the thread.

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

Parameters:
session - Session to close
See Also:
SessionFactoryUtils.closeSessionIfNecessary(net.sf.hibernate.Session, net.sf.hibernate.SessionFactory)


Copyright (C) 2003-2004 The Spring Framework Project.