|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.springframework.dao.support.DaoSupport org.springframework.orm.hibernate3.support.HibernateDaoSupport
public abstract class HibernateDaoSupport
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 releaseSession
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
.
setSessionFactory(org.hibernate.SessionFactory)
,
setHibernateTemplate(org.springframework.orm.hibernate3.HibernateTemplate)
,
createHibernateTemplate(org.hibernate.SessionFactory)
,
getSession()
,
releaseSession(org.hibernate.Session)
,
HibernateTemplate
,
HibernateInterceptor
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 void |
closeSessionIfNecessary(org.hibernate.Session session)
Deprecated. in favor of releaseSession |
protected DataAccessException |
convertHibernateAccessException(org.hibernate.HibernateException ex)
Convert the given HibernateException to an appropriate exception from the org.springframework.dao hierarchy. |
protected HibernateTemplate |
createHibernateTemplate(org.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 org.hibernate.Session |
getSession()
Get a Hibernate Session, either from the current transaction or a new one. |
protected org.hibernate.Session |
getSession(boolean allowCreate)
Get a Hibernate Session, either from the current transaction or a new one. |
org.hibernate.SessionFactory |
getSessionFactory()
Return the Hibernate SessionFactory used by this DAO. |
protected void |
releaseSession(org.hibernate.Session session)
Close the given Hibernate Session, created via this DAO's SessionFactory, if it isn't bound to the thread. |
void |
setHibernateTemplate(HibernateTemplate hibernateTemplate)
Set the HibernateTemplate for this DAO explicitly, as an alternative to specifying a SessionFactory. |
void |
setSessionFactory(org.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 |
---|
public HibernateDaoSupport()
Method Detail |
---|
public final void setSessionFactory(org.hibernate.SessionFactory sessionFactory)
createHibernateTemplate(org.hibernate.SessionFactory)
,
setHibernateTemplate(org.springframework.orm.hibernate3.HibernateTemplate)
protected HibernateTemplate createHibernateTemplate(org.hibernate.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 for
setSessionFactory(org.hibernate.SessionFactory)
public final org.hibernate.SessionFactory getSessionFactory()
public final void setHibernateTemplate(HibernateTemplate hibernateTemplate)
setSessionFactory(org.hibernate.SessionFactory)
public final HibernateTemplate getHibernateTemplate()
protected final void checkDaoConfig()
DaoSupport
Implementors should be marked as final
, to make it clear that
concrete subclasses are not supposed to override this template method themselves.
checkDaoConfig
in class DaoSupport
protected final org.hibernate.Session getSession() throws DataAccessResourceFailureException, IllegalStateException
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
releaseSession
.
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.
DataAccessResourceFailureException
- if the Session couldn't be created
IllegalStateException
- if no thread-bound Session found and allowCreate falsereleaseSession(org.hibernate.Session)
,
SessionFactoryUtils.getSession(SessionFactory, boolean)
,
HibernateInterceptor
,
HibernateTemplate
,
HibernateCallback
protected final org.hibernate.Session getSession(boolean allowCreate) throws DataAccessResourceFailureException, IllegalStateException
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
releaseSession
.
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.
allowCreate
- if a non-transactional Session should be created when no
transactional Session can be found for the current thread
DataAccessResourceFailureException
- if the Session couldn't be created
IllegalStateException
- if no thread-bound Session found and allowCreate falsereleaseSession(org.hibernate.Session)
,
SessionFactoryUtils.getSession(SessionFactory, boolean)
,
HibernateInterceptor
,
HibernateTemplate
,
HibernateCallback
protected final DataAccessException convertHibernateAccessException(org.hibernate.HibernateException ex)
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 releaseSession
.
ex
- HibernateException that occured
setHibernateTemplate(org.springframework.orm.hibernate3.HibernateTemplate)
,
getSession()
,
releaseSession(org.hibernate.Session)
,
HibernateAccessor.convertHibernateAccessException(org.hibernate.HibernateException)
protected final void closeSessionIfNecessary(org.hibernate.Session session)
releaseSession(org.hibernate.Session)
protected final void releaseSession(org.hibernate.Session session)
Typically used in plain Hibernate code, in combination with
getSession
and convertHibernateAccessException
.
session
- Session to closeSessionFactoryUtils.releaseSession(org.hibernate.Session, org.hibernate.SessionFactory)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |