Package org.springframework.orm.hibernate5.support

# Class HibernateDaoSupport

java.lang.Object
org.springframework.dao.support.DaoSupport
org.springframework.orm.hibernate5.support.HibernateDaoSupport

All Implemented Interfaces:
  `org.springframework.beans.factory.InitializingBean`

public abstract class HibernateDaoSupport extends org.springframework.dao.support.DaoSupport
Convenient superclass 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 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(org.hibernate.SessionFactory)`.

NOTE: Hibernate access code can also be coded in plain Hibernate style. Hence, for newly started projects, consider adopting the standard Hibernate style of coding data access objects instead, based on `SessionFactory.getCurrentSession()`. This HibernateTemplate primarily exists as a migration helper for Hibernate 3 based data access code, to benefit from bug fixes in Hibernate 5.x.

Since:
  4.2
Author:
  Juergen Hoeller
See Also:
  - `setSessionFactory(org.hibernate.SessionFactory)`
  - `getHibernateTemplate()`
  - `HibernateTemplate`

## Field Summary

### Fields inherited from class org.springframework.dao.support.DaoSupport

`logger`

## Constructor Summary

Constructors
Constructor
Description
`HibernateDaoSupport()`

## Method Summary

Modifier and Type
Method
Description
`protected final void`
`checkDaoConfig()`
`protected HibernateTemplate`
`createHibernateTemplate(SessionFactory sessionFactory)`
Create a HibernateTemplate for the given SessionFactory.
`protected final Session`
`currentSession()`
Conveniently obtain the current Hibernate Session.
`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.

### 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 Details

### HibernateDaoSupport

public HibernateDaoSupport()

## Method Details

### 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(org.hibernate.SessionFactory)`
  - `setHibernateTemplate(org.springframework.orm.hibernate5.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(org.hibernate.SessionFactory)`

### getSessionFactory

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

### setHibernateTemplate

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

See Also:
  - `setSessionFactory(org.hibernate.SessionFactory)`

### getHibernateTemplate

@Nullable 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()

Specified by:
  `checkDaoConfig` in class `org.springframework.dao.support.DaoSupport`

### currentSession

protected final Session currentSession() throws org.springframework.dao.DataAccessResourceFailureException
Conveniently obtain the current Hibernate Session.

Returns:
  the Hibernate Session
Throws:
  `org.springframework.dao.DataAccessResourceFailureException` - if the Session couldn't be created
See Also:
  - `SessionFactory.getCurrentSession()`