Overview | Package | Class | Use | Tree | Deprecated | Index | Help
| The Spring Framework
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## org.springframework.orm.hibernate
Class HibernateInterceptor

    java.lang.Object
      org.springframework.orm.hibernate.HibernateAccessor
          org.springframework.orm.hibernate.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, to be able to detect a thread-bound Session. It is preferable to use `getSession` with allowCreate=false, if the code relies on the interceptor to provide proper Session handling. Typically, the code will look like as follows:

     public void doSomeDataAccessAction() {
       Session session = SessionFactoryUtils.getSession(this.sessionFactory, false);
       try {
         ...
       }
       catch (HibernateException ex) {
         throw SessionFactoryUtils.convertHibernateAccessException(ex);
       }
     }

Note that the application must care about handling HibernateExceptions itself, preferably 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).

Unfortunately, this interceptor cannot convert checked HibernateExceptions to unchecked dao ones transparently. The intercepted method would have to declare the checked HibernateException - thus the caller would still have to catch or rethrow it, even if it will never be thrown if intercepted. Any such exception will nevertheless get converted by default.

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 drawbacks are:

- the dependency on interceptor configuration;
- the delegating try/catch blocks.

Note: Spring's Hibernate support in this package requires Hibernate 2.1. Dedicated Hibernate3 support can be found in a separate package: `org.springframework.orm.hibernate3`.

Since:
  13.06.2003
Author:
  Juergen Hoeller
See Also:
  `SessionFactoryUtils.getSession(net.sf.hibernate.SessionFactory, boolean)`, `HibernateTransactionManager`, `HibernateTemplate`

Field Summary

Fields inherited from class org.springframework.orm.hibernate.HibernateAccessor
`FLUSH_AUTO, FLUSH_COMMIT, FLUSH_EAGER, FLUSH_NEVER, logger`

Constructor Summary
`HibernateInterceptor()`

Method Summary
`protected net.sf.hibernate.Session` | `getSession()` Return a Session for use by this interceptor.
`Object` | `invoke(MethodInvocation methodInvocation)`
`void` | `setExceptionConversionEnabled(boolean exceptionConversionEnabled)` Set whether to convert any HibernateException raised to a Spring DataAccessException, compatible with the `org.springframework.dao` exception hierarchy.

Methods inherited from class org.springframework.orm.hibernate.HibernateAccessor
`afterPropertiesSet, applyFlushMode, convertHibernateAccessException, convertJdbcAccessException, convertJdbcAccessException, flushIfNecessary, getEntityInterceptor, getFlushMode, getJdbcExceptionTranslator, getSessionFactory, setBeanFactory, setEntityInterceptor, setEntityInterceptorBeanName, 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

### setExceptionConversionEnabled

    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. Note that this means that the DAO methods will have to declare the checked HibernateException, and callers will be forced to handle it.

  See Also:
    `DataAccessException`

### invoke

    public Object invoke(MethodInvocation methodInvocation)
                  throws Throwable

  Specified by:
    `invoke` in interface `MethodInterceptor`
  Throws:
    `Throwable`

### getSession

    protected net.sf.hibernate.Session getSession()

  Return a Session for use by this interceptor.

  See Also:
    `SessionFactoryUtils.getSession(net.sf.hibernate.SessionFactory, boolean)`

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
| The Spring Framework
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

Copyright © 2002-2007 The Spring Framework.