Package org.springframework.orm.hibernate5.support

# 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:
  - `OpenSessionInViewInterceptor`
  - `OpenSessionInViewFilter`
  - `HibernateTransactionManager`
  - `TransactionSynchronizationManager`
  - `SessionFactory.getCurrentSession()`

## Constructor Summary

Constructors
Constructor
Description
`OpenSessionInterceptor()`

## Method Summary

Modifier and Type
Method
Description
`void`
`afterPropertiesSet()`
`SessionFactory`
`getSessionFactory()`
Return the Hibernate SessionFactory that should be used to create Hibernate Sessions.
`Object`
`invoke(org.aopalliance.intercept.MethodInvocation invocation)`
`protected Session`
`openSession(SessionFactory sessionFactory)`
Open a Session for the given SessionFactory.
`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 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:
  - `FlushMode.MANUAL`