Save This Page
Home » Spring-Framework-090522 » org.springframework » orm » hibernate3 » [javadoc | source]
org.springframework.orm.hibernate3
public class: HibernateInterceptor [javadoc | source]
java.lang.Object
   org.springframework.orm.hibernate3.HibernateAccessor
      org.springframework.orm.hibernate3.HibernateInterceptor

All Implemented Interfaces:
    org.aopalliance.intercept.MethodInterceptor, BeanFactoryAware, InitializingBean

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 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.

Fields inherited from org.springframework.orm.hibernate3.HibernateAccessor:
FLUSH_NEVER,  FLUSH_AUTO,  FLUSH_EAGER,  FLUSH_COMMIT,  FLUSH_ALWAYS,  logger
Method from org.springframework.orm.hibernate3.HibernateInterceptor Summary:
getSession,   invoke,   setExceptionConversionEnabled
Methods from 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
Methods from java.lang.Object:
clone,   equals,   finalize,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from org.springframework.orm.hibernate3.HibernateInterceptor Detail:
 protected Session getSession() 
    Return a Session for use by this interceptor.
 public Object invoke(MethodInvocation methodInvocation) throws Throwable 
 public  void setExceptionConversionEnabled(boolean exceptionConversionEnabled) 
    Set whether to convert any HibernateException raised to a Spring DataAccessException, compatible with the org.springframework.dao exception hierarchy.

    Default is "true". Turn this flag off to let the caller receive raw exceptions as-is, without any wrapping.