org.springframework.orm.jpa

## Class JpaInterceptor

- java.lang.Object
- - org.springframework.orm.jpa.EntityManagerFactoryAccessor
  - - org.springframework.orm.jpa.JpaAccessor
    - - org.springframework.orm.jpa.JpaInterceptor

All Implemented Interfaces:
  Advice, Interceptor, MethodInterceptor, Aware, BeanFactoryAware, InitializingBean

Deprecated.
as of Spring 3.1, in favor of native EntityManager usage (typically obtained through `@PersistenceContext`) and AOP-driven exception translation through `PersistenceExceptionTranslationPostProcessor`

    @Deprecated
    public class JpaInterceptor
    extends JpaAccessor
    implements MethodInterceptor

This interceptor binds a new JPA EntityManager to the thread before a method call, closing and removing it afterwards in case of any method outcome. If there already is a pre-bound EntityManager (e.g. from JpaTransactionManager, or from a surrounding JPA-intercepted method), the interceptor simply participates in it.

Application code must retrieve a JPA EntityManager via the `EntityManagerFactoryUtils.getEntityManager` method or - preferably - via a shared `EntityManager` reference, to be able to detect a thread-bound EntityManager. Typically, the code will look like as follows:

     public void doSomeDataAccessAction() {
       this.entityManager...
     }

Note that this interceptor automatically translates PersistenceExceptions, via delegating to the `EntityManagerFactoryUtils.convertJpaAccessException` method that converts them to exceptions that are compatible with the `org.springframework.dao` exception hierarchy (like JpaTemplate does).

This class can be considered a declarative alternative to JpaTemplate's callback approach. The advantages are:

- no anonymous classes necessary for callback implementations;
- the possibility to throw any application exceptions from within data access code.

The drawback is the dependency on interceptor configuration. However, note that this interceptor is usually not necessary in scenarios where the data access code always executes within transactions. A transaction will always have a thread-bound EntityManager in the first place, so adding this interceptor to the configuration just adds value when fine-tuning EntityManager settings like the flush mode - or when relying on exception translation.

Since:
  2.0
Author:
  Juergen Hoeller
See Also:
  `JpaTransactionManager`, `JpaTemplate`

### Field Summary

### Fields inherited from class org.springframework.orm.jpa.EntityManagerFactoryAccessor

`logger`

### Constructor Summary

Constructors
Constructor and Description
`JpaInterceptor()` Deprecated.

### Method Summary

Methods
`Object` `invoke(MethodInvocation methodInvocation)` Deprecated.
`void` `setExceptionConversionEnabled(boolean exceptionConversionEnabled)` Deprecated. Set whether to convert any PersistenceException raised to a Spring DataAccessException, compatible with the `org.springframework.dao` exception hierarchy.

### Methods inherited from class org.springframework.orm.jpa.JpaAccessor

`afterPropertiesSet, flushIfNecessary, getEntityManager, getJpaDialect, isFlushEager, setEntityManager, setFlushEager, setJpaDialect, translateIfNecessary`

### Methods inherited from class org.springframework.orm.jpa.EntityManagerFactoryAccessor

`createEntityManager, getEntityManagerFactory, getJpaPropertyMap, getPersistenceUnitName, getTransactionalEntityManager, setBeanFactory, setEntityManagerFactory, setJpaProperties, setJpaPropertyMap, setPersistenceUnitName`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Constructor Detail

#### JpaInterceptor

    public JpaInterceptor()

Deprecated.

### Method Detail

#### setExceptionConversionEnabled

    public void setExceptionConversionEnabled(boolean exceptionConversionEnabled)

Deprecated.
Set whether to convert any PersistenceException raised to a Spring DataAccessException, compatible with the `org.springframework.dao` exception hierarchy.

Default is "true". Turn this flag off to let the caller receive raw exceptions as-is, without any wrapping.

See Also:
  `DataAccessException`

#### invoke

    public Object invoke(MethodInvocation methodInvocation)
                  throws Throwable

Deprecated.

Specified by:
  `invoke` in interface `MethodInterceptor`
Throws:
  `Throwable`