Class OpenSessionInterceptor

java.lang.Object
org.springframework.orm.hibernate5.support.OpenSessionInterceptor
All Implemented Interfaces:
org.aopalliance.aop.Advice, org.aopalliance.intercept.Interceptor, org.aopalliance.intercept.MethodInterceptor, org.springframework.beans.factory.InitializingBean

public class OpenSessionInterceptor extends Object implements org.aopalliance.intercept.MethodInterceptor, org.springframework.beans.factory.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.2
Author:
Juergen Hoeller
See Also:
  • Constructor Details

    • OpenSessionInterceptor

      public OpenSessionInterceptor()
  • Method Details

    • setSessionFactory

      public void setSessionFactory(@Nullable SessionFactory sessionFactory)
      Set the Hibernate SessionFactory that should be used to create Hibernate Sessions.
    • getSessionFactory

      @Nullable public SessionFactory getSessionFactory()
      Return the Hibernate SessionFactory that should be used to create Hibernate Sessions.
    • afterPropertiesSet

      public void afterPropertiesSet()
      Specified by:
      afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
    • invoke

      @Nullable public Object invoke(org.aopalliance.intercept.MethodInvocation invocation) throws Throwable
      Specified by:
      invoke in interface org.aopalliance.intercept.MethodInterceptor
      Throws:
      Throwable
    • openSession

      protected Session openSession(SessionFactory sessionFactory) throws org.springframework.dao.DataAccessResourceFailureException
      Open a Session for the given SessionFactory.

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

      Parameters:
      sessionFactory - the SessionFactory to use
      Returns:
      the Session to use
      Throws:
      org.springframework.dao.DataAccessResourceFailureException - if the Session could not be created
      Since:
      5.0
      See Also: