public abstract class HibernateDaoSupport extends DaoSupport
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.
setSessionFactory(org.hibernate.SessionFactory)
,
getHibernateTemplate()
,
HibernateTemplate
logger
Constructor and Description |
---|
HibernateDaoSupport() |
Modifier and Type | Method and Description |
---|---|
protected void |
checkDaoConfig()
Abstract subclasses must override this to check their configuration.
|
protected HibernateTemplate |
createHibernateTemplate(SessionFactory sessionFactory)
Create a HibernateTemplate for the given SessionFactory.
|
protected Session |
currentSession()
Conveniently obtain the current Hibernate Session.
|
HibernateTemplate |
getHibernateTemplate()
Return the HibernateTemplate for this DAO,
pre-initialized with the SessionFactory or set explicitly.
|
SessionFactory |
getSessionFactory()
Return the Hibernate SessionFactory used by this DAO.
|
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.
|
afterPropertiesSet, initDao
public final void setSessionFactory(SessionFactory sessionFactory)
protected HibernateTemplate createHibernateTemplate(SessionFactory sessionFactory)
Can be overridden in subclasses to provide a HibernateTemplate instance with different configuration, or a custom HibernateTemplate subclass.
sessionFactory
- the Hibernate SessionFactory to create a HibernateTemplate forsetSessionFactory(org.hibernate.SessionFactory)
@Nullable public final SessionFactory getSessionFactory()
public final void setHibernateTemplate(@Nullable HibernateTemplate hibernateTemplate)
@Nullable public final HibernateTemplate getHibernateTemplate()
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.
protected final void checkDaoConfig()
DaoSupport
Implementors should be marked as final
if concrete subclasses
are not supposed to override this template method themselves.
checkDaoConfig
in class DaoSupport
protected final Session currentSession() throws DataAccessResourceFailureException
DataAccessResourceFailureException
- if the Session couldn't be createdSessionFactory.getCurrentSession()