Class Overview
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.
Summary
Public Methods |
Object
|
invoke(MethodInvocation methodInvocation)
|
void
|
setExceptionConversionEnabled(boolean exceptionConversionEnabled)
Set whether to convert any PersistenceException raised to a Spring DataAccessException,
compatible with the org.springframework.dao exception hierarchy.
|
[Expand]
Inherited Methods |
From class
org.springframework.orm.jpa.JpaAccessor
void
|
afterPropertiesSet()
Eagerly initialize the JPA dialect, creating a default one
for the specified EntityManagerFactory if none set.
|
void
|
flushIfNecessary(EntityManager em, boolean existingTransaction)
Flush the given JPA entity manager if necessary.
|
EntityManager
|
getEntityManager()
Return the JPA EntityManager to use.
|
JpaDialect
|
getJpaDialect()
Return the JPA dialect to use for this accessor.
|
boolean
|
isFlushEager()
Return if this accessor should flush changes to the database eagerly.
|
void
|
setEntityManager(EntityManager entityManager)
Set the JPA EntityManager to use.
|
void
|
setFlushEager(boolean flushEager)
Set if this accessor should flush changes to the database eagerly.
|
void
|
setJpaDialect(JpaDialect jpaDialect)
Set the JPA dialect to use for this accessor.
|
RuntimeException
|
translateIfNecessary(RuntimeException ex)
Convert the given runtime exception to an appropriate exception from the
org.springframework.dao hierarchy if necessary, or
return the exception itself if it is not persistence related
Default implementation delegates to the JpaDialect.
|
|
From class
org.springframework.orm.jpa.EntityManagerFactoryAccessor
EntityManager
|
createEntityManager()
Obtain a new EntityManager from this accessor's EntityManagerFactory.
|
EntityManagerFactory
|
getEntityManagerFactory()
Return the JPA EntityManagerFactory that should be used to create
EntityManagers.
|
Map<String, Object>
|
getJpaPropertyMap()
Allow Map access to the JPA properties to be passed to the persistence
provider, with the option to add or override specific entries.
|
EntityManager
|
getTransactionalEntityManager()
Obtain the transactional EntityManager for this accessor's EntityManagerFactory, if any.
|
void
|
setEntityManagerFactory(EntityManagerFactory emf)
Set the JPA EntityManagerFactory that should be used to create
EntityManagers.
|
void
|
setJpaProperties(Properties jpaProperties)
Specify JPA properties, to be passed into
EntityManagerFactory.createEntityManager(Map) (if any).
|
void
|
setJpaPropertyMap(Map<String, Object> jpaProperties)
Specify JPA properties as a Map, to be passed into
EntityManagerFactory.createEntityManager(Map) (if any).
|
|
From class
java.lang.Object
Object
|
clone()
|
boolean
|
equals(Object arg0)
|
void
|
finalize()
|
final
Class<?>
|
getClass()
|
int
|
hashCode()
|
final
void
|
notify()
|
final
void
|
notifyAll()
|
String
|
toString()
|
final
void
|
wait()
|
final
void
|
wait(long arg0, int arg1)
|
final
void
|
wait(long arg0)
|
|
From interface
org.springframework.beans.factory.InitializingBean
abstract
void
|
afterPropertiesSet()
Invoked by a BeanFactory after it has set all bean properties supplied
(and satisfied BeanFactoryAware and ApplicationContextAware).
|
|
Public Constructors
Public Methods
public
Object
invoke
(MethodInvocation methodInvocation)
public
void
setExceptionConversionEnabled
(boolean exceptionConversionEnabled)
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.