Home » Spring-Framework-090522 » org.springframework » orm » hibernate » support » [javadoc | source]
org.springframework.orm.hibernate.support
abstract public class: HibernateDaoSupport [javadoc | source]
java.lang.Object
   org.springframework.orm.hibernate.support.HibernateDaoSupport

All Implemented Interfaces:
    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.

Field Summary
protected final  Log logger     
Method from org.springframework.orm.hibernate.support.HibernateDaoSupport Summary:
afterPropertiesSet,   closeSessionIfNecessary,   convertHibernateAccessException,   createHibernateTemplate,   getHibernateTemplate,   getSession,   getSession,   getSessionFactory,   initDao,   setHibernateTemplate,   setSessionFactory
Methods from java.lang.Object:
clone,   equals,   finalize,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from org.springframework.orm.hibernate.support.HibernateDaoSupport Detail:
 public final  void afterPropertiesSet() throws Exception 
 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.

 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.

 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.

 public final HibernateTemplate getHibernateTemplate() 
    Return the HibernateTemplate for this DAO, pre-initialized with the SessionFactory or set explicitly.
 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.

 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.

 public final SessionFactory getSessionFactory() 
    Return the Hibernate SessionFactory used by this DAO.
 protected  void initDao() throws Exception 
    Subclasses can override this for custom initialization behavior. Gets called after population of this instance's bean properties.
 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.