org.springframework.orm.hibernate3
Class HibernateInterceptor

java.lang.Object
  extended by org.springframework.orm.hibernate3.HibernateAccessor
      extended by org.springframework.orm.hibernate3.HibernateInterceptor
All Implemented Interfaces:
Advice, Interceptor, MethodInterceptor, BeanFactoryAware, InitializingBean

public class HibernateInterceptor
extends HibernateAccessor
implements MethodInterceptor

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 own SessionFactory.getCurrentSession() method, to be able to detect a thread-bound Session. Typically, the code will look like as follows:

 public void doHibernateAction() {
   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 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.

Since:
1.2
Author:
Juergen Hoeller
See Also:
SessionFactory.getCurrentSession(), HibernateTransactionManager, HibernateTemplate

Field Summary
 
Fields inherited from class org.springframework.orm.hibernate3.HibernateAccessor
FLUSH_ALWAYS, FLUSH_AUTO, FLUSH_COMMIT, FLUSH_EAGER, FLUSH_NEVER, logger
 
Constructor Summary
HibernateInterceptor()
           
 
Method Summary
protected  Session getSession()
          Return a Session for use by this interceptor.
 Object invoke(MethodInvocation methodInvocation)
           
 
Methods inherited from class org.springframework.orm.hibernate3.HibernateAccessor
afterPropertiesSet, applyFlushMode, convertHibernateAccessException, convertJdbcAccessException, convertJdbcAccessException, disableFilters, enableFilters, flushIfNecessary, getEntityInterceptor, getFilterNames, getFlushMode, getJdbcExceptionTranslator, getSessionFactory, setBeanFactory, setEntityInterceptor, setEntityInterceptorBeanName, setFilterName, setFilterNames, setFlushMode, setFlushModeName, setJdbcExceptionTranslator, setSessionFactory
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

HibernateInterceptor

public HibernateInterceptor()
Method Detail

invoke

public Object invoke(MethodInvocation methodInvocation)
              throws Throwable
Specified by:
invoke in interface MethodInterceptor
Throws:
Throwable

getSession

protected Session getSession()
Return a Session for use by this interceptor.

See Also:
SessionFactoryUtils.getSession(org.hibernate.SessionFactory, boolean)


Copyright (c) 2002-2007 The Spring Framework Project.