类 HibernateInterceptor
- java.lang.Object
- org.springframework.orm.hibernate3.HibernateAccessor
- org.springframework.orm.hibernate3.HibernateInterceptor
- 所有已实现的接口:
Advice
,Interceptor
,MethodInterceptor
,Aware
,BeanFactoryAware
,InitializingBean
@Deprecated public class HibernateInterceptor extends HibernateAccessor implements MethodInterceptor
已过时。as of Spring 3.2.7, in favor of either HibernateTemplate usage or native Hibernate API usage within transactions, in combination with a generalPersistenceExceptionTranslationPostProcessor
. Note: This class does not have an equivalent replacement inorm.hibernate4
. If you desperately need a scoped Session bound through AOP, consider the newly introducedOpenSessionInterceptor
.This interceptor binds a new Hibernate Session to the thread before a method call, closing and removing it afterwards in case of any method outcome. If there already is a pre-bound Session (e.g. from HibernateTransactionManager, or from a surrounding Hibernate-intercepted method), the interceptor simply participates in it.Application code must retrieve a Hibernate Session via the
SessionFactoryUtils.getSession
method or - preferably - Hibernate's ownSessionFactory.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 theSessionFactoryUtils.convertHibernateAccessException
method that converts them to exceptions that are compatible with theorg.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:
- no anonymous classes necessary for callback implementations;
- the possibility to throw any application exceptions from within data access code.
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.
- 从以下版本开始:
- 1.2
- 作者:
- Juergen Hoeller
- 另请参阅:
SessionFactory.getCurrentSession()
,HibernateTransactionManager
,HibernateTemplate
字段概要
从类继承的字段 org.springframework.orm.hibernate3.HibernateAccessor
FLUSH_ALWAYS, FLUSH_AUTO, FLUSH_COMMIT, FLUSH_EAGER, FLUSH_NEVER, logger
构造器概要
构造器 构造器 说明 HibernateInterceptor()
已过时。
方法概要
所有方法 实例方法 具体方法 已过时的方法 修饰符和类型 方法 说明 protected Session
getSession()
已过时。Return a Session for use by this interceptor.Object
invoke(MethodInvocation methodInvocation)
已过时。Implement this method to perform extra treatments before and after the invocation.void
setExceptionConversionEnabled(boolean exceptionConversionEnabled)
已过时。Set whether to convert any HibernateException raised to a Spring DataAccessException, compatible with theorg.springframework.dao
exception hierarchy.从类继承的方法 org.springframework.orm.hibernate3.HibernateAccessor
afterPropertiesSet, applyFlushMode, convertHibernateAccessException, convertJdbcAccessException, convertJdbcAccessException, disableFilters, enableFilters, flushIfNecessary, getDefaultJdbcExceptionTranslator, getEntityInterceptor, getFilterNames, getFlushMode, getJdbcExceptionTranslator, getSessionFactory, setBeanFactory, setEntityInterceptor, setEntityInterceptorBeanName, setFilterName, setFilterNames, setFlushMode, setFlushModeName, setJdbcExceptionTranslator, setSessionFactory
构造器详细资料
HibernateInterceptor
public HibernateInterceptor()
已过时。
方法详细资料
setExceptionConversionEnabled
public void setExceptionConversionEnabled(boolean exceptionConversionEnabled)
已过时。Set whether to convert any HibernateException raised to a Spring DataAccessException, compatible with theorg.springframework.dao
exception hierarchy.Default is "true". Turn this flag off to let the caller receive raw exceptions as-is, without any wrapping.
- 另请参阅:
DataAccessException
invoke
public Object invoke(MethodInvocation methodInvocation) throws Throwable
已过时。从接口复制的说明:MethodInterceptor
Implement this method to perform extra treatments before and after the invocation. Polite implementations would certainly like to invokeJoinpoint.proceed()
.- 指定者:
invoke
在接口中MethodInterceptor
- 参数:
methodInvocation
- the method invocation joinpoint- 返回:
- the result of the call to
Joinpoint.proceed()
; might be intercepted by the interceptor - 抛出:
Throwable
- if the interceptors or the target object throws an exception
getSession
protected Session getSession()
已过时。Return a Session for use by this interceptor.