Top Description Fields Constructors Methods
org.springframework.orm.hibernate5.support

public abstract Class HibernateDaoSupport

extends DaoSupport
Class Inheritance
Imports
org.hibernate.Session, .SessionFactory, org.springframework.dao.DataAccessResourceFailureException, org.springframework.dao.support.DaoSupport, org.springframework.lang.Nullable, org.springframework.orm.hibernate5.HibernateTemplate, org.springframework.util.Assert

Convenient superclass for Hibernate-based data access objects.

Requires a SessionFactory to be set, providing a org.springframework.orm.hibernate5.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.

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.

Author
Juergen Hoeller
Since
4.2
See Also
setSessionFactory, getHibernateTemplate, org.springframework.orm.hibernate5.HibernateTemplate

Field Summary

Modifier and TypeField and Description
private HibernateTemplate
Inherited from org.springframework.dao.support.DaoSupport:
logger

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected final void
checkDaoConfig()

Implements abstract org.springframework.dao.support.DaoSupport.checkDaoConfig.

Abstract subclasses must override this to check their configuration.

protected HibernateTemplate

Returns:

the new HibernateTemplate instance
createHibernateTemplate
(SessionFactory
the Hibernate SessionFactory to create a HibernateTemplate for
sessionFactory
)
References Deprecated SessionFactory is deprecated or references (maybe indirectly) at least one deprecated element.

Create a HibernateTemplate for the given SessionFactory.

protected final Session

Returns:

the Hibernate Session
currentSession
()
References Deprecated Session is deprecated or references (maybe indirectly) at least one deprecated element.

Conveniently obtain the current Hibernate Session.

public final HibernateTemplate
getHibernateTemplate()

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

public final SessionFactory
getSessionFactory()
References Deprecated SessionFactory is deprecated or references (maybe indirectly) at least one deprecated element.

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)
References Deprecated SessionFactory is deprecated or references (maybe indirectly) at least one deprecated element.

Set the Hibernate SessionFactory to be used by this DAO.

Inherited from org.springframework.dao.support.DaoSupport:
afterPropertiesSetinitDao

Field Detail

hibernateTemplateback to summary
private HibernateTemplate hibernateTemplate
Annotations
@Nullable

Constructor Detail

HibernateDaoSupportback to summary
public HibernateDaoSupport()

Method Detail

checkDaoConfigback to summary
protected final void checkDaoConfig()

Implements abstract org.springframework.dao.support.DaoSupport.checkDaoConfig.

Doc from org.springframework.dao.support.DaoSupport.checkDaoConfig.

Abstract subclasses must override this to check their configuration.

Implementors should be marked as final if concrete subclasses are not supposed to override this template method themselves.

Annotations
@Override
createHibernateTemplateback to summary
protected HibernateTemplate createHibernateTemplate(SessionFactory sessionFactory)

References Deprecated

SessionFactory is deprecated or references (maybe indirectly) at least one deprecated element.

See corresponding docs for further information.

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:SessionFactory

the Hibernate SessionFactory to create a HibernateTemplate for

Returns:HibernateTemplate

the new HibernateTemplate instance

See Also
setSessionFactory
currentSessionback to summary
protected final Session currentSession() throws DataAccessResourceFailureException

References Deprecated

Session is deprecated or references (maybe indirectly) at least one deprecated element.

See corresponding docs for further information.

Conveniently obtain the current Hibernate Session.

Returns:Session

the Hibernate Session

Exceptions
DataAccessResourceFailureException:
if the Session couldn't be created
See Also
SessionFactory#getCurrentSession()
getHibernateTemplateback to summary
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.

Annotations
@Nullable
getSessionFactoryback to summary
public final SessionFactory getSessionFactory()

References Deprecated

SessionFactory is deprecated or references (maybe indirectly) at least one deprecated element.

See corresponding docs for further information.

Return the Hibernate SessionFactory used by this DAO.

Annotations
@Nullable
setHibernateTemplateback to summary
public final void setHibernateTemplate(HibernateTemplate hibernateTemplate)

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

See Also
setSessionFactory
setSessionFactoryback to summary
public final void setSessionFactory(SessionFactory sessionFactory)

References Deprecated

SessionFactory is deprecated or references (maybe indirectly) at least one deprecated element.

See corresponding docs for further information.

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

See Also
createHibernateTemplate, setHibernateTemplate