Top Description Fields Constructors Methods
org.springframework.orm.hibernate5.support

public Class OpenSessionInterceptor

extends Object
implements MethodInterceptor, InitializingBean
Class Inheritance
All Implemented Interfaces
org.springframework.beans.factory.InitializingBean, org.aopalliance.intercept.MethodInterceptor, org.aopalliance.intercept.Interceptor, org.aopalliance.aop.Advice
Imports
org.aopalliance.intercept.MethodInterceptor, .MethodInvocation, org.hibernate.FlushMode, .HibernateException, .Session, .SessionFactory, org.springframework.beans.factory.InitializingBean, org.springframework.dao.DataAccessResourceFailureException, org.springframework.lang.Nullable, org.springframework.orm.hibernate5.SessionFactoryUtils, .SessionHolder, org.springframework.transaction.support.TransactionSynchronizationManager, org.springframework.util.Assert

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.

Author
Juergen Hoeller
Since
4.2
See Also
OpenSessionInViewInterceptor, OpenSessionInViewFilter, org.springframework.orm.hibernate5.HibernateTransactionManager, TransactionSynchronizationManager, SessionFactory#getCurrentSession()

Field Summary

Modifier and TypeField and Description
private SessionFactory
sessionFactory

Deprecated as a consequence of SessionFactory being deprecated.

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
afterPropertiesSet()

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Invoked by the containing BeanFactory after it has set all bean properties and satisfied BeanFactoryAware, ApplicationContextAware etc.

This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.

public SessionFactory
getSessionFactory()

Deprecated as a consequence of SessionFactory being deprecated.
Return the Hibernate SessionFactory that should be used to create Hibernate Sessions.
public Object
invoke(MethodInvocation
the method invocation joinpoint
invocation
)

Implements org.aopalliance.intercept.MethodInterceptor.invoke.

Implement this method to perform extra treatments before and after the invocation.
protected Session

Returns:

the Session to use
openSession
(SessionFactory
the SessionFactory to use
sessionFactory
)

Deprecated as a consequence of SessionFactory and Session being deprecated.
Open a Session for the given SessionFactory.
public void
setSessionFactory(SessionFactory sessionFactory)

Deprecated as a consequence of SessionFactory being deprecated.
Set the Hibernate SessionFactory that should be used to create Hibernate Sessions.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

sessionFactoryback to summary
private SessionFactory sessionFactory

Deprecated

as a consequence of SessionFactory being deprecated. See corresponding docs for further information.

Annotations
@Nullable

Constructor Detail

OpenSessionInterceptorback to summary
public OpenSessionInterceptor()

Method Detail

afterPropertiesSetback to summary
public void afterPropertiesSet()

Implements org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Doc from org.springframework.beans.factory.InitializingBean.afterPropertiesSet.

Invoked by the containing BeanFactory after it has set all bean properties and satisfied BeanFactoryAware, ApplicationContextAware etc.

This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.

Annotations
@Override
getSessionFactoryback to summary
public SessionFactory getSessionFactory()

Deprecated

as a consequence of SessionFactory being deprecated. See corresponding docs for further information.

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

Annotations
@Nullable
invokeback to summary
public Object invoke(MethodInvocation invocation) throws Throwable

Implements org.aopalliance.intercept.MethodInterceptor.invoke.

Doc from org.aopalliance.intercept.MethodInterceptor.invoke.

Implement this method to perform extra treatments before and after the invocation. Polite implementations would certainly like to invoke Joinpoint#proceed().

Parameters
invocation:MethodInvocation

the method invocation joinpoint

Returns:Object

the result of the call to Joinpoint#proceed(); might be intercepted by the interceptor

Annotations
@Override
@Nullable
Exceptions
Throwable:
if the interceptors or the target object throws an exception
openSessionback to summary
protected Session openSession(SessionFactory sessionFactory) throws DataAccessResourceFailureException

Deprecated

as a consequence of SessionFactory and Session being deprecated. See corresponding docs for further information.

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:SessionFactory

the SessionFactory to use

Returns:Session

the Session to use

Exceptions
DataAccessResourceFailureException:
if the Session could not be created
Since
5.0
See Also
FlushMode#MANUAL
setSessionFactoryback to summary
public void setSessionFactory(SessionFactory sessionFactory)

Deprecated

as a consequence of SessionFactory being deprecated. See corresponding docs for further information.

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