PersistenceExceptionTranslationPostProcessor
.
Note: This class does not have an equivalent replacement in orm.hibernate4
.
If you desperately need a scoped Session bound through AOP, consider the newly
introduced OpenSessionInterceptor
.@Deprecated public class HibernateInterceptor extends HibernateAccessor implements MethodInterceptor
Application code must retrieve a Hibernate Session via the
SessionFactoryUtils.getSession
method or - preferably -
Hibernate's own SessionFactory.getCurrentSession()
method, to be
able to detect a thread-bound Session. Typically, the code will look like as follows:
public void doSomeDataAccessAction() { Session session = this.sessionFactory.getCurrentSession(); ... // No need to close the Session or translate exceptions! }Note that this interceptor automatically translates HibernateExceptions, via delegating to the
SessionFactoryUtils.convertHibernateAccessException
method that converts them to exceptions that are compatible with the
org.springframework.dao
exception hierarchy (like HibernateTemplate does).
This can be turned off if the raw exceptions are preferred.
This class can be considered a declarative alternative to HibernateTemplate's callback approach. The advantages are:
The drawback is the dependency on interceptor configuration. However, note that this interceptor is usually not necessary in scenarios where the data access code always executes within transactions. A transaction will always have a thread-bound Session in the first place, so adding this interceptor to the configuration just adds value when fine-tuning Session settings like the flush mode - or when relying on exception translation.
SessionFactory.getCurrentSession()
,
HibernateTransactionManager
,
HibernateTemplate
FLUSH_ALWAYS, FLUSH_AUTO, FLUSH_COMMIT, FLUSH_EAGER, FLUSH_NEVER, logger
Constructor and Description |
---|
HibernateInterceptor()
Deprecated.
|
Modifier and Type | Method and Description |
---|---|
protected Session |
getSession()
Deprecated.
Return a Session for use by this interceptor.
|
java.lang.Object |
invoke(MethodInvocation methodInvocation)
Deprecated.
Implement this method to perform extra treatments before and
after the invocation.
|
void |
setExceptionConversionEnabled(boolean exceptionConversionEnabled)
Deprecated.
Set whether to convert any HibernateException raised to a Spring DataAccessException,
compatible with the
org.springframework.dao exception hierarchy. |
afterPropertiesSet, applyFlushMode, convertHibernateAccessException, convertJdbcAccessException, convertJdbcAccessException, disableFilters, enableFilters, flushIfNecessary, getDefaultJdbcExceptionTranslator, getEntityInterceptor, getFilterNames, getFlushMode, getJdbcExceptionTranslator, getSessionFactory, setBeanFactory, setEntityInterceptor, setEntityInterceptorBeanName, setFilterName, setFilterNames, setFlushMode, setFlushModeName, setJdbcExceptionTranslator, setSessionFactory
public void setExceptionConversionEnabled(boolean exceptionConversionEnabled)
org.springframework.dao
exception hierarchy.
Default is "true". Turn this flag off to let the caller receive raw exceptions as-is, without any wrapping.
DataAccessException
public java.lang.Object invoke(MethodInvocation methodInvocation) throws java.lang.Throwable
MethodInterceptor
Joinpoint.proceed()
.invoke
in interface MethodInterceptor
methodInvocation
- the method invocation joinpointJoinpoint.proceed()
;
might be intercepted by the interceptorjava.lang.Throwable
- if the interceptors or the target object
throws an exceptionprotected Session getSession()