站内搜索: 请输入搜索关键词
当前页面: 在线文档首页 > Spring Framework 2.1.0 API 文档英文版

HibernateDaoSupport (Spring Framework API 2.1) - Spring Framework 2.1.0 API 文档英文版


The Spring Framework

org.springframework.orm.hibernate.support
Class HibernateDaoSupport

java.lang.Object
  extended by org.springframework.dao.support.DaoSupport
      extended by org.springframework.orm.hibernate.support.HibernateDaoSupport
All Implemented Interfaces:
InitializingBean

public abstract class HibernateDaoSupport
extends DaoSupport

Convenient super class for Hibernate-based data access objects.

Requires a 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(net.sf.hibernate.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(net.sf.hibernate.SessionFactory).

Since:
28.07.2003
Author:
Juergen Hoeller
See Also:
setSessionFactory(net.sf.hibernate.SessionFactory), getHibernateTemplate(), HibernateTemplate

Field Summary
 
Fields inherited from class org.springframework.dao.support.DaoSupport
logger
 
Constructor Summary
HibernateDaoSupport()
           
 
Method Summary
protected  void checkDaoConfig()
          Abstract subclasses must override this to check their configuration.
protected  DataAccessException convertHibernateAccessException(net.sf.hibernate.HibernateException ex)
          Convert the given HibernateException to an appropriate exception from the org.springframework.dao hierarchy.
protected  HibernateTemplate createHibernateTemplate(net.sf.hibernate.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  net.sf.hibernate.Session getSession()
          Obtain a Hibernate Session, either from the current transaction or a new one.
protected  net.sf.hibernate.Session getSession(boolean allowCreate)
          Obtain a Hibernate Session, either from the current transaction or a new one.
 net.sf.hibernate.SessionFactory getSessionFactory()
          Return the Hibernate SessionFactory used by this DAO.
protected  void releaseSession(net.sf.hibernate.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).
 void setHibernateTemplate(HibernateTemplate hibernateTemplate)
          Set the HibernateTemplate for this DAO explicitly, as an alternative to specifying a SessionFactory.
 void setSessionFactory(net.sf.hibernate.SessionFactory sessionFactory)
          Set the Hibernate SessionFactory to be used by this DAO.
 
Methods inherited from class org.springframework.dao.support.DaoSupport
afterPropertiesSet, initDao
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HibernateDaoSupport

public HibernateDaoSupport()
Method Detail

setSessionFactory

public final void setSessionFactory(net.sf.hibernate.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(net.sf.hibernate.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 net.sf.hibernate.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.

Note: The returned HibernateTemplate is a shared instance. You may introspect its configuration, but not modify the configuration (other than from within an DaoSupport.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.


checkDaoConfig

protected final void checkDaoConfig()
Description copied from class: DaoSupport
Abstract subclasses must override this to check their configuration.

Implementors should be marked as final

Specified by:
checkDaoConfig in class DaoSupport

getSession

protected final net.sf.hibernate.Session getSession()
                                             throws DataAccessResourceFailureException,
                                                    IllegalStateException
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(net.sf.hibernate.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
See Also:
SessionFactoryUtils.getSession(SessionFactory, boolean)

getSession

protected final net.sf.hibernate.Session getSession(boolean allowCreate)
                                             throws DataAccessResourceFailureException,
                                                    IllegalStateException
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(net.sf.hibernate.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
See Also:
SessionFactoryUtils.getSession(SessionFactory, boolean)

convertHibernateAccessException

protected final DataAccessException convertHibernateAccessException(net.sf.hibernate.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 HibernateAccessor.convertHibernateAccessException(net.sf.hibernate.HibernateException) method of this DAO's HibernateTemplate.

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

Parameters:
ex - HibernateException that occured
Returns:
the corresponding DataAccessException instance
See Also:
HibernateAccessor.convertHibernateAccessException(net.sf.hibernate.HibernateException)

releaseSession

protected final void releaseSession(net.sf.hibernate.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(net.sf.hibernate.HibernateException).

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

The Spring Framework

Copyright © 2002-2007 The Spring Framework.