spring-framework / org.springframework.orm.jpa.support / OpenEntityManagerInViewInterceptor

OpenEntityManagerInViewInterceptor

open class OpenEntityManagerInViewInterceptor : EntityManagerFactoryAccessor, 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 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
OpenEntityManagerInViewFilterorg.springframework.orm.jpa.JpaTransactionManagerorg.springframework.orm.jpa.SharedEntityManagerCreatororg.springframework.transaction.support.TransactionSynchronizationManager

Constructors

<init>

OpenEntityManagerInViewInterceptor()

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.

Properties

PARTICIPATE_SUFFIX

static val PARTICIPATE_SUFFIX: String

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

Functions

afterCompletion

open fun afterCompletion(request: WebRequest, ex: Exception): Unit

afterConcurrentHandlingStarted

open fun afterConcurrentHandlingStarted(request: WebRequest): Unit

postHandle

open fun postHandle(request: WebRequest, model: ModelMap): Unit

preHandle

open fun preHandle(request: WebRequest): Unit

Inherited Functions

getEntityManagerFactory

open fun getEntityManagerFactory(): EntityManagerFactory

Return the JPA EntityManagerFactory that should be used to create EntityManagers.

getJpaPropertyMap

open fun getJpaPropertyMap(): MutableMap<String, Any>

Allow Map access to the JPA properties to be passed to the persistence provider, with the option to add or override specific entries.

Useful for specifying entries directly, for example via "jpaPropertyMap[myKey]".

getPersistenceUnitName

open fun getPersistenceUnitName(): String

Return the name of the persistence unit to access the EntityManagerFactory for, if any.

setBeanFactory

open fun setBeanFactory(beanFactory: BeanFactory): Unit

Retrieves an EntityManagerFactory by persistence unit name, if none set explicitly. Falls back to a default EntityManagerFactory bean if no persistence unit specified.

setEntityManagerFactory

open fun setEntityManagerFactory(emf: EntityManagerFactory): Unit

Set the JPA EntityManagerFactory that should be used to create EntityManagers.

setJpaProperties

open fun setJpaProperties(jpaProperties: Properties): Unit

Specify JPA properties, to be passed into EntityManagerFactory.createEntityManager(Map) (if any).

Can be populated with a String "value" (parsed via PropertiesEditor) or a "props" element in XML bean definitions.

setJpaPropertyMap

open fun setJpaPropertyMap(jpaProperties: MutableMap<String, Any>): Unit

Specify JPA properties as a Map, to be passed into EntityManagerFactory.createEntityManager(Map) (if any).

Can be populated with a "map" or "props" element in XML bean definitions.

setPersistenceUnitName

open fun setPersistenceUnitName(persistenceUnitName: String): Unit

Set the name of the persistence unit to access the EntityManagerFactory for.

This is an alternative to specifying the EntityManagerFactory by direct reference, resolving it by its persistence unit name instead. If no EntityManagerFactory and no persistence unit name have been specified, a default EntityManagerFactory will be retrieved through finding a single unique bean of type EntityManagerFactory.