Class OpenEntityManagerInViewInterceptor

java.lang.Object
org.springframework.orm.jpa.EntityManagerFactoryAccessor
org.springframework.orm.jpa.support.OpenEntityManagerInViewInterceptor
All Implemented Interfaces:
Aware, BeanFactoryAware, AsyncWebRequestInterceptor, WebRequestInterceptor

public class OpenEntityManagerInViewInterceptor extends EntityManagerFactoryAccessor implements AsyncWebRequestInterceptor
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 JpaTransactionManager or 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.

Since:
2.0
Author:
Juergen Hoeller
See Also:
  • Field Details

    • PARTICIPATE_SUFFIX

      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:
  • Constructor Details

    • OpenEntityManagerInViewInterceptor

      public OpenEntityManagerInViewInterceptor()
  • Method Details

    • preHandle

      public void preHandle(WebRequest request) throws DataAccessException
      Description copied from interface: WebRequestInterceptor
      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.

      Specified by:
      preHandle in interface WebRequestInterceptor
      Parameters:
      request - the current web request
      Throws:
      DataAccessException
    • postHandle

      public void postHandle(WebRequest request, @Nullable ModelMap model)
      Description copied from interface: WebRequestInterceptor
      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).

      Specified by:
      postHandle in interface WebRequestInterceptor
      Parameters:
      request - the current web request
      model - 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.
    • afterCompletion

      public void afterCompletion(WebRequest request, @Nullable Exception ex) throws DataAccessException
      Description copied from interface: WebRequestInterceptor
      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!

      Specified by:
      afterCompletion in interface WebRequestInterceptor
      Parameters:
      request - the current web request
      ex - exception thrown on handler execution, if any
      Throws:
      DataAccessException
    • afterConcurrentHandlingStarted

      public void afterConcurrentHandlingStarted(WebRequest request)
      Description copied from interface: AsyncWebRequestInterceptor
      Called instead of postHandle and afterCompletion, when the handler started handling the request concurrently.
      Specified by:
      afterConcurrentHandlingStarted in interface AsyncWebRequestInterceptor
      Parameters:
      request - the current request
    • getParticipateAttributeName

      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: