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

public Class OpenEntityManagerInViewInterceptor

extends EntityManagerFactoryAccessor
implements AsyncWebRequestInterceptor
Class Inheritance
All Implemented Interfaces
org.springframework.web.context.request.AsyncWebRequestInterceptor, org.springframework.web.context.request.WebRequestInterceptor
Imports
jakarta.persistence.EntityManager, .EntityManagerFactory, .PersistenceException, org.springframework.dao.DataAccessException, .DataAccessResourceFailureException, org.springframework.lang.Nullable, org.springframework.orm.jpa.EntityManagerFactoryAccessor, .EntityManagerFactoryUtils, .EntityManagerHolder, org.springframework.transaction.support.TransactionSynchronizationManager, org.springframework.ui.ModelMap, org.springframework.web.context.request.AsyncWebRequestInterceptor, .WebRequest, org.springframework.web.context.request.async.CallableProcessingInterceptor, .WebAsyncManager, .WebAsyncUtils

Spring web request interceptor that binds a JPA EntityManager to the thread for the entire processing of the request. Intended for the "Open EntityManager in View" pattern, i.e. to allow for lazy loading in web views despite the original transactions already being completed.

This interceptor makes JPA EntityManagers available via the current thread, which will be autodetected by transaction managers. It is suitable for service layer transactions via org.springframework.orm.jpa.JpaTransactionManager or org.springframework.transaction.jta.JtaTransactionManager as well as for non-transactional read-only execution.

In contrast to OpenEntityManagerInViewFilter, this interceptor is set up in a Spring application context and can thus take advantage of bean wiring.

Author
Juergen Hoeller
Since
2.0
See Also
OpenEntityManagerInViewFilter, org.springframework.orm.jpa.JpaTransactionManager, org.springframework.orm.jpa.SharedEntityManagerCreator, org.springframework.transaction.support.TransactionSynchronizationManager

Field Summary

Modifier and TypeField and Description
public static final String
PARTICIPATE_SUFFIX

Suffix that gets appended to the EntityManagerFactory toString representation for the "participate in existing entity manager handling" request attribute.

Inherited from org.springframework.orm.jpa.EntityManagerFactoryAccessor:
logger

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
afterCompletion(WebRequest
the current web request
request
,
Exception
exception thrown on handler execution, if any
ex
)

Implements org.springframework.web.context.request.WebRequestInterceptor.afterCompletion.

Callback after completion of request processing, that is, after rendering the view.

public void
afterConcurrentHandlingStarted(WebRequest
the current request
request
)

Implements org.springframework.web.context.request.AsyncWebRequestInterceptor.afterConcurrentHandlingStarted.

Called instead of postHandle and afterCompletion, when the handler started handling the request concurrently.

private boolean
private boolean
protected String
getParticipateAttributeName()

Return the name of the request attribute that identifies that a request is already filtered.

public void
postHandle(WebRequest
the current web request
request
,
ModelMap
the map of model objects that will be exposed to the view (may be null). Can be used to analyze the exposed model and/or to add further model attributes, if desired.
model
)

Implements org.springframework.web.context.request.WebRequestInterceptor.postHandle.

Intercept the execution of a request handler after its successful invocation, right before view rendering (if any).

public void
preHandle(WebRequest
the current web request
request
)

Implements org.springframework.web.context.request.WebRequestInterceptor.preHandle.

Intercept the execution of a request handler before its invocation.

Inherited from org.springframework.orm.jpa.EntityManagerFactoryAccessor:
createEntityManagergetEntityManagerFactorygetJpaPropertyMapgetPersistenceUnitNamegetTransactionalEntityManagerobtainEntityManagerFactorysetBeanFactorysetEntityManagerFactorysetJpaPropertiessetJpaPropertyMapsetPersistenceUnitName

Field Detail

PARTICIPATE_SUFFIXback to summary
public static final String PARTICIPATE_SUFFIX

Suffix that gets appended to the EntityManagerFactory toString representation for the "participate in existing entity manager handling" request attribute.

See Also
getParticipateAttributeName

Constructor Detail

OpenEntityManagerInViewInterceptorback to summary
public OpenEntityManagerInViewInterceptor()

Method Detail

afterCompletionback to summary
public void afterCompletion(WebRequest request, Exception ex) throws DataAccessException

Implements org.springframework.web.context.request.WebRequestInterceptor.afterCompletion.

Doc from org.springframework.web.context.request.WebRequestInterceptor.afterCompletion.

Callback after completion of request processing, that is, after rendering the view. Will be called on any outcome of handler execution, thus allows for proper resource cleanup.

Note

Will only be called if this interceptor's preHandle method has successfully completed!

Parameters
request:WebRequest

the current web request

ex:Exception

exception thrown on handler execution, if any

Annotations
@Override
afterConcurrentHandlingStartedback to summary
public void afterConcurrentHandlingStarted(WebRequest request)

Implements org.springframework.web.context.request.AsyncWebRequestInterceptor.afterConcurrentHandlingStarted.

Doc from org.springframework.web.context.request.AsyncWebRequestInterceptor.afterConcurrentHandlingStarted.

Called instead of postHandle and afterCompletion, when the handler started handling the request concurrently.

Parameters
request:WebRequest

the current request

Annotations
@Override
applyEntityManagerBindingInterceptorback to summary
private boolean applyEntityManagerBindingInterceptor(WebAsyncManager asyncManager, String key)
decrementParticipateCountback to summary
private boolean decrementParticipateCount(WebRequest request)
getParticipateAttributeNameback to summary
protected String getParticipateAttributeName()

Return the name of the request attribute that identifies that a request is already filtered. Default implementation takes the toString representation of the EntityManagerFactory instance and appends ".FILTERED".

See Also
PARTICIPATE_SUFFIX
postHandleback to summary
public void postHandle(WebRequest request, ModelMap model)

Implements org.springframework.web.context.request.WebRequestInterceptor.postHandle.

Doc from org.springframework.web.context.request.WebRequestInterceptor.postHandle.

Intercept the execution of a request handler after its successful invocation, right before view rendering (if any).

Allows for modifying context resources after successful handler execution (for example, flushing a Hibernate Session).

Parameters
request:WebRequest

the current web request

model:ModelMap

the map of model objects that will be exposed to the view (may be null). Can be used to analyze the exposed model and/or to add further model attributes, if desired.

Annotations
@Override
preHandleback to summary
public void preHandle(WebRequest request) throws DataAccessException

Implements org.springframework.web.context.request.WebRequestInterceptor.preHandle.

Doc from org.springframework.web.context.request.WebRequestInterceptor.preHandle.

Intercept the execution of a request handler before its invocation.

Allows for preparing context resources (such as a Hibernate Session) and expose them as request attributes or as thread-local objects.

Parameters
request:WebRequest

the current web request

Annotations
@Override