org.springframework.orm.hibernate4.support

## Class OpenSessionInterceptor

- java.lang.Object
- - org.springframework.orm.hibernate4.support.OpenSessionInterceptor

- All Implemented Interfaces:
    Advice, Interceptor, MethodInterceptor, InitializingBean

      public class OpenSessionInterceptor
      extends Object
      implements MethodInterceptor, InitializingBean

  Simple AOP Alliance `MethodInterceptor` implementation that binds a new Hibernate `Session` for each method invocation, if none bound before.

  This is a simple Hibernate Session scoping interceptor along the lines of `OpenSessionInViewInterceptor`, just for use with AOP setup instead of MVC setup. It opens a new `Session` with flush mode "MANUAL" since the Session is only meant for reading, except when participating in a transaction.

  Since:
    4.0.2
  Author:
    Juergen Hoeller
  See Also:
    `OpenSessionInViewInterceptor`, `OpenSessionInViewFilter`, `HibernateTransactionManager`, `TransactionSynchronizationManager`, `SessionFactory.getCurrentSession()`

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `OpenSessionInterceptor()`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `afterPropertiesSet()` Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).
    `SessionFactory` | `getSessionFactory()` Return the Hibernate SessionFactory that should be used to create Hibernate Sessions.
    `Object` | `invoke(MethodInvocation invocation)`
    `protected Session` | `openSession()` Open a Session for the SessionFactory that this interceptor uses.
    `void` | `setSessionFactory(SessionFactory sessionFactory)` Set the Hibernate SessionFactory that should be used to create Hibernate Sessions.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### OpenSessionInterceptor

          public OpenSessionInterceptor()

  - ### Method Detail

    - #### setSessionFactory

          public void setSessionFactory(SessionFactory sessionFactory)

      Set the Hibernate SessionFactory that should be used to create Hibernate Sessions.

    - #### getSessionFactory

          public SessionFactory getSessionFactory()

      Return the Hibernate SessionFactory that should be used to create Hibernate Sessions.

    - #### afterPropertiesSet

          public void afterPropertiesSet()

      Description copied from interface: `InitializingBean`
      Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).

      This method allows the bean instance to perform initialization only possible when all bean properties have been set and to throw an exception in the event of misconfiguration.

      Specified by:
        `afterPropertiesSet` in interface `InitializingBean`

    - #### invoke

          public Object invoke(MethodInvocation invocation)
                        throws Throwable

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

    - #### openSession

          protected Session openSession()
                                 throws DataAccessResourceFailureException

      Open a Session for the SessionFactory that this interceptor uses.

      The default implementation delegates to the `SessionFactory.openSession()` method and sets the `Session`'s flush mode to "MANUAL".

      Returns:
        the Session to use
      Throws:
        `DataAccessResourceFailureException` - if the Session could not be created
      See Also:
        `FlushMode.MANUAL`