public abstract class EntityManagerFactoryUtils
extends java.lang.Object
Mainly intended for internal use within the framework.
Modifier and Type | Class and Description |
---|---|
private static class |
EntityManagerFactoryUtils.EntityManagerSynchronization
Callback for resource cleanup at the end of a non-JPA transaction
(e.g.
|
Modifier and Type | Field and Description |
---|---|
static int |
ENTITY_MANAGER_SYNCHRONIZATION_ORDER
Order value for TransactionSynchronization objects that clean up JPA
EntityManagers.
|
private static Log |
logger |
Constructor and Description |
---|
EntityManagerFactoryUtils() |
Modifier and Type | Method and Description |
---|---|
static void |
applyTransactionTimeout(Query query,
EntityManagerFactory emf)
Apply the current transaction timeout, if any, to the given JPA Query object.
|
private static void |
cleanupTransaction(java.lang.Object transactionData,
EntityManagerFactory emf)
Prepare a transaction on the given EntityManager, if possible.
|
static void |
closeEntityManager(EntityManager em)
Close the given JPA EntityManager,
catching and logging any cleanup exceptions thrown.
|
static DataAccessException |
convertJpaAccessExceptionIfPossible(java.lang.RuntimeException ex)
Convert the given runtime exception to an appropriate exception from the
org.springframework.dao hierarchy. |
static EntityManager |
doGetTransactionalEntityManager(EntityManagerFactory emf,
java.util.Map properties)
Obtain a JPA EntityManager from the given factory.
|
static EntityManagerFactory |
findEntityManagerFactory(ListableBeanFactory beanFactory,
java.lang.String unitName)
Find an EntityManagerFactory with the given name in the given
Spring application context (represented as ListableBeanFactory).
|
static EntityManager |
getTransactionalEntityManager(EntityManagerFactory emf)
Obtain a JPA EntityManager from the given factory.
|
static EntityManager |
getTransactionalEntityManager(EntityManagerFactory emf,
java.util.Map properties)
Obtain a JPA EntityManager from the given factory.
|
private static java.lang.Object |
prepareTransaction(EntityManager em,
EntityManagerFactory emf)
Prepare a transaction on the given EntityManager, if possible.
|
public static final int ENTITY_MANAGER_SYNCHRONIZATION_ORDER
private static final Log logger
public static EntityManagerFactory findEntityManagerFactory(ListableBeanFactory beanFactory, java.lang.String unitName) throws NoSuchBeanDefinitionException
The specified unit name will be matched against the configured
persistence unit, provided that a discovered EntityManagerFactory
implements the EntityManagerFactoryInfo
interface. If not,
the persistence unit name will be matched against the Spring bean name,
assuming that the EntityManagerFactory bean names follow that convention.
If no unit name has been given, this method will search for a default
EntityManagerFactory through BeanFactory.getBean(Class)
.
beanFactory
- the ListableBeanFactory to searchunitName
- the name of the persistence unit (may be null
or empty,
in which case a single bean of type EntityManagerFactory will be searched for)NoSuchBeanDefinitionException
- if there is no such EntityManagerFactory in the contextEntityManagerFactoryInfo.getPersistenceUnitName()
public static EntityManager getTransactionalEntityManager(EntityManagerFactory emf) throws DataAccessResourceFailureException
Note: Will return null
if no thread-bound EntityManager found!
emf
- EntityManagerFactory to create the EntityManager withnull
if none foundDataAccessResourceFailureException
- if the EntityManager couldn't be obtainedJpaTransactionManager
public static EntityManager getTransactionalEntityManager(EntityManagerFactory emf, java.util.Map properties) throws DataAccessResourceFailureException
Note: Will return null
if no thread-bound EntityManager found!
emf
- EntityManagerFactory to create the EntityManager withproperties
- the properties to be passed into the createEntityManager
call (may be null
)null
if none foundDataAccessResourceFailureException
- if the EntityManager couldn't be obtainedJpaTransactionManager
public static EntityManager doGetTransactionalEntityManager(EntityManagerFactory emf, java.util.Map properties) throws PersistenceException
Same as getEntityManager
, but throwing the original PersistenceException.
emf
- EntityManagerFactory to create the EntityManager withproperties
- the properties to be passed into the createEntityManager
call (may be null
)null
if none foundjavax.persistence.PersistenceException
- if the EntityManager couldn't be createdPersistenceException
#getTransactionalEntityManager(javax.persistence.EntityManagerFactory)
,
JpaTransactionManager
private static java.lang.Object prepareTransaction(EntityManager em, EntityManagerFactory emf)
em
- the EntityManager to prepareemf
- the EntityManagerFactory that the EntityManager has been created withJpaDialect.prepareTransaction(EntityManager, boolean, java.lang.String)
private static void cleanupTransaction(java.lang.Object transactionData, EntityManagerFactory emf)
transactionData
- arbitrary object that holds transaction data, if any
(as returned by prepareTransaction)emf
- the EntityManagerFactory that the EntityManager has been created withJpaDialect.cleanupTransaction(java.lang.Object)
public static void applyTransactionTimeout(Query query, EntityManagerFactory emf)
This method sets the JPA 2.0 query hint "javax.persistence.query.timeout" accordingly.
query
- the JPA Query objectemf
- JPA EntityManagerFactory that the Query was created forpublic static DataAccessException convertJpaAccessExceptionIfPossible(java.lang.RuntimeException ex)
org.springframework.dao
hierarchy.
Return null if no translation is appropriate: any other exception may
have resulted from user code, and should not be translated.
The most important cases like object not found or optimistic locking failure are covered here. For more fine-granular conversion, JpaTransactionManager etc support sophisticated translation of exceptions via a JpaDialect.
ex
- runtime exception that occurrednull
if the exception should not be translatedpublic static void closeEntityManager(EntityManager em)
em
- the JPA EntityManager to close (may be null
)javax.persistence.EntityManager#close()