类 EntityManagerFactoryUtils
- java.lang.Object
- org.springframework.orm.jpa.EntityManagerFactoryUtils
public abstract class EntityManagerFactoryUtils extends Object
Helper class featuring methods for JPA EntityManager handling, allowing for reuse of EntityManager instances within transactions. Also provides support for exception translation.Mainly intended for internal use within the framework.
- 从以下版本开始:
- 2.0
- 作者:
- Juergen Hoeller
字段概要
字段 修饰符和类型 字段 说明 static int
ENTITY_MANAGER_SYNCHRONIZATION_ORDER
Order value for TransactionSynchronization objects that clean up JPA EntityManagers.
构造器概要
构造器 构造器 说明 EntityManagerFactoryUtils()
方法概要
所有方法 静态方法 具体方法 修饰符和类型 方法 说明 static void
applyTransactionTimeout(Query query, EntityManagerFactory emf)
Apply the current transaction timeout, if any, to the given JPA Query object.static void
closeEntityManager(EntityManager em)
Close the given JPA EntityManager, catching and logging any cleanup exceptions thrown.static DataAccessException
convertJpaAccessExceptionIfPossible(RuntimeException ex)
Convert the given runtime exception to an appropriate exception from theorg.springframework.dao
hierarchy.static EntityManager
doGetTransactionalEntityManager(EntityManagerFactory emf, Map<?,?> properties)
Obtain a JPA EntityManager from the given factory.static EntityManager
doGetTransactionalEntityManager(EntityManagerFactory emf, Map<?,?> properties, boolean synchronizedWithTransaction)
Obtain a JPA EntityManager from the given factory.static EntityManagerFactory
findEntityManagerFactory(ListableBeanFactory beanFactory, 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, Map<?,?> properties)
Obtain a JPA EntityManager from the given factory.
字段详细资料
ENTITY_MANAGER_SYNCHRONIZATION_ORDER
public static final int ENTITY_MANAGER_SYNCHRONIZATION_ORDER
Order value for TransactionSynchronization objects that clean up JPA EntityManagers. Return DataSourceUtils.CONNECTION_SYNCHRONIZATION_ORDER - 100 to execute EntityManager cleanup before JDBC Connection cleanup, if any.
构造器详细资料
EntityManagerFactoryUtils
public EntityManagerFactoryUtils()
方法详细资料
findEntityManagerFactory
public static EntityManagerFactory findEntityManagerFactory(ListableBeanFactory beanFactory, String unitName) throws NoSuchBeanDefinitionException
Find an EntityManagerFactory with the given name in the given Spring application context (represented as ListableBeanFactory).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 benull
or empty, in which case a single bean of type EntityManagerFactory will be searched for)- 返回:
- the EntityManagerFactory
- 抛出:
NoSuchBeanDefinitionException
- if there is no such EntityManagerFactory in the context- 另请参阅:
EntityManagerFactoryInfo.getPersistenceUnitName()
getTransactionalEntityManager
public static EntityManager getTransactionalEntityManager(EntityManagerFactory emf) throws DataAccessResourceFailureException
Obtain a JPA EntityManager from the given factory. Is aware of a corresponding EntityManager bound to the current thread, e.g. when using JpaTransactionManager.Note: Will return
null
if no thread-bound EntityManager found!- 参数:
emf
- the EntityManagerFactory to create the EntityManager with- 返回:
- the EntityManager, or
null
if none found - 抛出:
DataAccessResourceFailureException
- if the EntityManager couldn't be obtained- 另请参阅:
JpaTransactionManager
getTransactionalEntityManager
public static EntityManager getTransactionalEntityManager(EntityManagerFactory emf, Map<?,?> properties) throws DataAccessResourceFailureException
Obtain a JPA EntityManager from the given factory. Is aware of a corresponding EntityManager bound to the current thread, e.g. when using JpaTransactionManager.Note: Will return
null
if no thread-bound EntityManager found!- 参数:
emf
- the EntityManagerFactory to create the EntityManager withproperties
- the properties to be passed into thecreateEntityManager
call (may benull
)- 返回:
- the EntityManager, or
null
if none found - 抛出:
DataAccessResourceFailureException
- if the EntityManager couldn't be obtained- 另请参阅:
JpaTransactionManager
doGetTransactionalEntityManager
public static EntityManager doGetTransactionalEntityManager(EntityManagerFactory emf, Map<?,?> properties) throws PersistenceException
Obtain a JPA EntityManager from the given factory. Is aware of a corresponding EntityManager bound to the current thread, e.g. when using JpaTransactionManager.Same as
getEntityManager
, but throwing the original PersistenceException.- 参数:
emf
- the EntityManagerFactory to create the EntityManager withproperties
- the properties to be passed into thecreateEntityManager
call (may benull
)- 返回:
- the EntityManager, or
null
if none found - 抛出:
PersistenceException
- if the EntityManager couldn't be created- 另请参阅:
getTransactionalEntityManager(javax.persistence.EntityManagerFactory)
,JpaTransactionManager
doGetTransactionalEntityManager
public static EntityManager doGetTransactionalEntityManager(EntityManagerFactory emf, Map<?,?> properties, boolean synchronizedWithTransaction) throws PersistenceException
Obtain a JPA EntityManager from the given factory. Is aware of a corresponding EntityManager bound to the current thread, e.g. when using JpaTransactionManager.Same as
getEntityManager
, but throwing the original PersistenceException.- 参数:
emf
- the EntityManagerFactory to create the EntityManager withproperties
- the properties to be passed into thecreateEntityManager
call (may benull
)synchronizedWithTransaction
- whether to automatically join ongoing transactions (according to the JPA 2.1 SynchronizationType rules)- 返回:
- the EntityManager, or
null
if none found - 抛出:
PersistenceException
- if the EntityManager couldn't be created- 另请参阅:
getTransactionalEntityManager(javax.persistence.EntityManagerFactory)
,JpaTransactionManager
applyTransactionTimeout
public static void applyTransactionTimeout(Query query, EntityManagerFactory emf)
Apply the current transaction timeout, if any, to the given JPA Query object.This method sets the JPA 2.0 query hint "javax.persistence.query.timeout" accordingly.
- 参数:
query
- the JPA Query objectemf
- the JPA EntityManagerFactory that the Query was created for
convertJpaAccessExceptionIfPossible
public static DataAccessException convertJpaAccessExceptionIfPossible(RuntimeException ex)
Convert the given runtime exception to an appropriate exception from theorg.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 occurred- 返回:
- the corresponding DataAccessException instance, or
null
if the exception should not be translated
closeEntityManager
public static void closeEntityManager(EntityManager em)
Close the given JPA EntityManager, catching and logging any cleanup exceptions thrown.- 参数:
em
- the JPA EntityManager to close (may benull
)- 另请参阅:
EntityManager.close()