类 AbstractEntityManagerFactoryBean
- java.lang.Object
- org.springframework.orm.jpa.AbstractEntityManagerFactoryBean
- 所有已实现的接口:
Serializable
,Aware
,BeanClassLoaderAware
,BeanFactoryAware
,BeanNameAware
,DisposableBean
,FactoryBean<EntityManagerFactory>
,InitializingBean
,PersistenceExceptionTranslator
,EntityManagerFactoryInfo
public abstract class AbstractEntityManagerFactoryBean extends Object implements FactoryBean<EntityManagerFactory>, BeanClassLoaderAware, BeanFactoryAware, BeanNameAware, InitializingBean, DisposableBean, EntityManagerFactoryInfo, PersistenceExceptionTranslator, Serializable
AbstractFactoryBean
that creates a local JPAEntityManagerFactory
instance within a Spring application context.Encapsulates the common functionality between the different JPA bootstrap contracts (standalone as well as container).
Implements support for standard JPA configuration conventions as well as Spring's customizable
JpaVendorAdapter
mechanism, and controls the EntityManagerFactory's lifecycle.This class also implements the
PersistenceExceptionTranslator
interface, as autodetected by Spring'sPersistenceExceptionTranslationPostProcessor
, for AOP-based translation of native exceptions to Spring DataAccessExceptions. Hence, the presence of e.g. LocalEntityManagerFactoryBean automatically enables a PersistenceExceptionTranslationPostProcessor to translate JPA exceptions.- 从以下版本开始:
- 2.0
- 作者:
- Juergen Hoeller, Rod Johnson
- 另请参阅:
LocalEntityManagerFactoryBean
,LocalContainerEntityManagerFactoryBean
, 序列化表格
构造器概要
构造器 构造器 说明 AbstractEntityManagerFactoryBean()
方法概要
所有方法 实例方法 抽象方法 具体方法 修饰符和类型 方法 说明 void
afterPropertiesSet()
Invoked by the containingBeanFactory
after it has set all bean properties and satisfiedBeanFactoryAware
,ApplicationContextAware
etc.protected EntityManagerFactory
createEntityManagerFactoryProxy(EntityManagerFactory emf)
Create a proxy for the givenEntityManagerFactory
.protected abstract EntityManagerFactory
createNativeEntityManagerFactory()
Subclasses must implement this method to create the EntityManagerFactory that will be returned by thegetObject()
method.void
destroy()
Close the EntityManagerFactory on bean factory shutdown.ClassLoader
getBeanClassLoader()
Return the ClassLoader that the application's beans are loaded with.AsyncTaskExecutor
getBootstrapExecutor()
Return the asynchronous executor for background bootstrapping, if any.DataSource
getDataSource()
Return the JDBC DataSource that this EntityManagerFactory obtains its JDBC Connections from.Class<? extends EntityManager>
getEntityManagerInterface()
Return the (potentially vendor-specific) EntityManager interface that this factory's EntityManagers will implement.JpaDialect
getJpaDialect()
Return the vendor-specific JpaDialect implementation for this EntityManagerFactory, ornull
if not known.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.JpaVendorAdapter
getJpaVendorAdapter()
Return the JpaVendorAdapter implementation for this EntityManagerFactory, ornull
if not known.EntityManagerFactory
getNativeEntityManagerFactory()
Return the raw underlying EntityManagerFactory.EntityManagerFactory
getObject()
Return the singleton EntityManagerFactory.Class<? extends EntityManagerFactory>
getObjectType()
Return the type of object that this FactoryBean creates, ornull
if not known in advance.PersistenceProvider
getPersistenceProvider()
Return the underlying PersistenceProvider that the underlying EntityManagerFactory was created with.PersistenceUnitInfo
getPersistenceUnitInfo()
Return the PersistenceUnitInfo used to create this EntityManagerFactory, if the in-container API was used.String
getPersistenceUnitName()
Return the name of the persistence unit used to create this EntityManagerFactory, ornull
if it is an unnamed default.boolean
isSingleton()
Is the object managed by this factory a singleton?void
setBeanClassLoader(ClassLoader classLoader)
Callback that supplies the beanclass loader
to a bean instance.void
setBeanFactory(BeanFactory beanFactory)
Callback that supplies the owning factory to a bean instance.void
setBeanName(String name)
Set the name of the bean in the bean factory that created this bean.void
setBootstrapExecutor(AsyncTaskExecutor bootstrapExecutor)
Specify an asynchronous executor for background bootstrapping, e.g. aSimpleAsyncTaskExecutor
.void
setEntityManagerFactoryInterface(Class<? extends EntityManagerFactory> emfInterface)
Specify the (potentially vendor-specific) EntityManagerFactory interface that this EntityManagerFactory proxy is supposed to implement.void
setEntityManagerInterface(Class<? extends EntityManager> emInterface)
Specify the (potentially vendor-specific) EntityManager interface that this factory's EntityManagers are supposed to implement.void
setJpaDialect(JpaDialect jpaDialect)
Specify the vendor-specific JpaDialect implementation to associate with this EntityManagerFactory.void
setJpaProperties(Properties jpaProperties)
Specify JPA properties, to be passed intoPersistence.createEntityManagerFactory
(if any).void
setJpaPropertyMap(Map<String,?> jpaProperties)
Specify JPA properties as a Map, to be passed intoPersistence.createEntityManagerFactory
(if any).void
setJpaVendorAdapter(JpaVendorAdapter jpaVendorAdapter)
Specify the JpaVendorAdapter implementation for the desired JPA provider, if any.void
setPersistenceProvider(PersistenceProvider persistenceProvider)
Set the PersistenceProvider instance to use for creating the EntityManagerFactory.void
setPersistenceProviderClass(Class<? extends PersistenceProvider> persistenceProviderClass)
Set the PersistenceProvider implementation class to use for creating the EntityManagerFactory.void
setPersistenceUnitName(String persistenceUnitName)
Specify the name of the EntityManagerFactory configuration.DataAccessException
translateExceptionIfPossible(RuntimeException ex)
Implementation of the PersistenceExceptionTranslator interface, as autodetected by Spring's PersistenceExceptionTranslationPostProcessor.protected Object
writeReplace()
构造器详细资料
AbstractEntityManagerFactoryBean
public AbstractEntityManagerFactoryBean()
方法详细资料
setPersistenceProviderClass
public void setPersistenceProviderClass(Class<? extends PersistenceProvider> persistenceProviderClass)
Set the PersistenceProvider implementation class to use for creating the EntityManagerFactory. If not specified, the persistence provider will be taken from the JpaVendorAdapter (if any) or retrieved through scanning (as far as possible).
setPersistenceProvider
public void setPersistenceProvider(PersistenceProvider persistenceProvider)
Set the PersistenceProvider instance to use for creating the EntityManagerFactory. If not specified, the persistence provider will be taken from the JpaVendorAdapter (if any) or determined by the persistence unit deployment descriptor (as far as possible).
getPersistenceProvider
public PersistenceProvider getPersistenceProvider()
从接口复制的说明:EntityManagerFactoryInfo
Return the underlying PersistenceProvider that the underlying EntityManagerFactory was created with.- 指定者:
getPersistenceProvider
在接口中EntityManagerFactoryInfo
- 返回:
- the PersistenceProvider used to create this EntityManagerFactory, or
null
if the standard JPA provider autodetection process was used to configure the EntityManagerFactory
setPersistenceUnitName
public void setPersistenceUnitName(String persistenceUnitName)
Specify the name of the EntityManagerFactory configuration.Default is none, indicating the default EntityManagerFactory configuration. The persistence provider will throw an exception if ambiguous EntityManager configurations are found.
getPersistenceUnitName
public String getPersistenceUnitName()
从接口复制的说明:EntityManagerFactoryInfo
Return the name of the persistence unit used to create this EntityManagerFactory, ornull
if it is an unnamed default.If
getPersistenceUnitInfo()
returns non-null, the result ofgetPersistenceUnitName()
must be equal to the value returned byPersistenceUnitInfo.getPersistenceUnitName()
.
setJpaProperties
public void setJpaProperties(Properties jpaProperties)
Specify JPA properties, to be passed intoPersistence.createEntityManagerFactory
(if any).Can be populated with a String "value" (parsed via PropertiesEditor) or a "props" element in XML bean definitions.
setJpaPropertyMap
public void setJpaPropertyMap(Map<String,?> jpaProperties)
Specify JPA properties as a Map, to be passed intoPersistence.createEntityManagerFactory
(if any).Can be populated with a "map" or "props" element in XML bean definitions.
getJpaPropertyMap
public 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.Useful for specifying entries directly, for example via "jpaPropertyMap[myKey]".
setEntityManagerFactoryInterface
public void setEntityManagerFactoryInterface(Class<? extends EntityManagerFactory> emfInterface)
Specify the (potentially vendor-specific) EntityManagerFactory interface that this EntityManagerFactory proxy is supposed to implement.The default will be taken from the specific JpaVendorAdapter, if any, or set to the standard
javax.persistence.EntityManagerFactory
interface else.
setEntityManagerInterface
public void setEntityManagerInterface(Class<? extends EntityManager> emInterface)
Specify the (potentially vendor-specific) EntityManager interface that this factory's EntityManagers are supposed to implement.The default will be taken from the specific JpaVendorAdapter, if any, or set to the standard
javax.persistence.EntityManager
interface else.
getEntityManagerInterface
public Class<? extends EntityManager> getEntityManagerInterface()
从接口复制的说明:EntityManagerFactoryInfo
Return the (potentially vendor-specific) EntityManager interface that this factory's EntityManagers will implement.A
null
return value suggests that autodetection is supposed to happen: either based on a targetEntityManager
instance or simply defaulting tojavax.persistence.EntityManager
.
setJpaDialect
public void setJpaDialect(JpaDialect jpaDialect)
Specify the vendor-specific JpaDialect implementation to associate with this EntityManagerFactory. This will be exposed through the EntityManagerFactoryInfo interface, to be picked up as default dialect by accessors that intend to use JpaDialect functionality.
getJpaDialect
public JpaDialect getJpaDialect()
从接口复制的说明:EntityManagerFactoryInfo
Return the vendor-specific JpaDialect implementation for this EntityManagerFactory, ornull
if not known.- 指定者:
getJpaDialect
在接口中EntityManagerFactoryInfo
setJpaVendorAdapter
public void setJpaVendorAdapter(JpaVendorAdapter jpaVendorAdapter)
Specify the JpaVendorAdapter implementation for the desired JPA provider, if any. This will initialize appropriate defaults for the given provider, such as persistence provider class and JpaDialect, unless locally overridden in this FactoryBean.
getJpaVendorAdapter
public JpaVendorAdapter getJpaVendorAdapter()
Return the JpaVendorAdapter implementation for this EntityManagerFactory, ornull
if not known.
setBootstrapExecutor
public void setBootstrapExecutor(AsyncTaskExecutor bootstrapExecutor)
Specify an asynchronous executor for background bootstrapping, e.g. aSimpleAsyncTaskExecutor
.EntityManagerFactory
initialization will then switch into background bootstrap mode, with aEntityManagerFactory
proxy immediately returned for injection purposes instead of waiting for the JPA provider's bootstrapping to complete. However, note that the first actual call to aEntityManagerFactory
method will then block until the JPA provider's bootstrapping completed, if not ready by then. For maximum benefit, make sure to avoid earlyEntityManagerFactory
calls in init methods of related beans, even for metadata introspection purposes.- 从以下版本开始:
- 4.3
getBootstrapExecutor
public AsyncTaskExecutor getBootstrapExecutor()
Return the asynchronous executor for background bootstrapping, if any.- 从以下版本开始:
- 4.3
setBeanClassLoader
public void setBeanClassLoader(ClassLoader classLoader)
从接口复制的说明:BeanClassLoaderAware
Callback that supplies the beanclass loader
to a bean instance.Invoked after the population of normal bean properties but before an initialization callback such as
InitializingBean's
InitializingBean.afterPropertiesSet()
method or a custom init-method.- 指定者:
setBeanClassLoader
在接口中BeanClassLoaderAware
- 参数:
classLoader
- the owning class loader; may benull
in which case a defaultClassLoader
must be used, for example theClassLoader
obtained viaClassUtils.getDefaultClassLoader()
getBeanClassLoader
public ClassLoader getBeanClassLoader()
从接口复制的说明:EntityManagerFactoryInfo
Return the ClassLoader that the application's beans are loaded with.Proxies will be generated in this ClassLoader.
setBeanFactory
public void setBeanFactory(BeanFactory beanFactory)
从接口复制的说明:BeanFactoryAware
Callback that supplies the owning factory to a bean instance.Invoked after the population of normal bean properties but before an initialization callback such as
InitializingBean.afterPropertiesSet()
or a custom init-method.- 指定者:
setBeanFactory
在接口中BeanFactoryAware
- 参数:
beanFactory
- owning BeanFactory (nevernull
). The bean can immediately call methods on the factory.- 另请参阅:
BeanInitializationException
setBeanName
public void setBeanName(String name)
从接口复制的说明:BeanNameAware
Set the name of the bean in the bean factory that created this bean.Invoked after population of normal bean properties but before an init callback such as
InitializingBean.afterPropertiesSet()
or a custom init-method.- 指定者:
setBeanName
在接口中BeanNameAware
- 参数:
name
- the name of the bean in the factory. Note that this name is the actual bean name used in the factory, which may differ from the originally specified name: in particular for inner bean names, the actual bean name might have been made unique through appending "#..." suffixes. Use theBeanFactoryUtils.originalBeanName(String)
method to extract the original bean name (without suffix), if desired.
afterPropertiesSet
public void afterPropertiesSet() throws PersistenceException
从接口复制的说明:InitializingBean
Invoked by the containingBeanFactory
after it has set all bean properties and satisfiedBeanFactoryAware
,ApplicationContextAware
etc.This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.
createEntityManagerFactoryProxy
protected EntityManagerFactory createEntityManagerFactoryProxy(EntityManagerFactory emf)
Create a proxy for the givenEntityManagerFactory
. We do this to be able to return a transaction-aware proxy for an application-managedEntityManager
.- 参数:
emf
- the EntityManagerFactory as returned by the persistence provider, if initialized already- 返回:
- the EntityManagerFactory proxy
createNativeEntityManagerFactory
protected abstract EntityManagerFactory createNativeEntityManagerFactory() throws PersistenceException
Subclasses must implement this method to create the EntityManagerFactory that will be returned by thegetObject()
method.- 返回:
- EntityManagerFactory instance returned by this FactoryBean
- 抛出:
PersistenceException
- if the EntityManager cannot be created
translateExceptionIfPossible
public DataAccessException translateExceptionIfPossible(RuntimeException ex)
Implementation of the PersistenceExceptionTranslator interface, as autodetected by Spring's PersistenceExceptionTranslationPostProcessor.Uses the dialect's conversion if possible; otherwise falls back to standard JPA exception conversion.
- 指定者:
translateExceptionIfPossible
在接口中PersistenceExceptionTranslator
- 参数:
ex
- a RuntimeException to translate- 返回:
- the corresponding DataAccessException (or
null
if the exception could not be translated, as in this case it may result from user code rather than from an actual persistence problem) - 另请参阅:
PersistenceExceptionTranslationPostProcessor
,PersistenceExceptionTranslator.translateExceptionIfPossible(java.lang.RuntimeException)
,EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(java.lang.RuntimeException)
getNativeEntityManagerFactory
public EntityManagerFactory getNativeEntityManagerFactory()
从接口复制的说明:EntityManagerFactoryInfo
Return the raw underlying EntityManagerFactory.- 指定者:
getNativeEntityManagerFactory
在接口中EntityManagerFactoryInfo
- 返回:
- the unadorned EntityManagerFactory (never
null
)
getPersistenceUnitInfo
public PersistenceUnitInfo getPersistenceUnitInfo()
从接口复制的说明:EntityManagerFactoryInfo
Return the PersistenceUnitInfo used to create this EntityManagerFactory, if the in-container API was used.- 指定者:
getPersistenceUnitInfo
在接口中EntityManagerFactoryInfo
- 返回:
- the PersistenceUnitInfo used to create this EntityManagerFactory, or
null
if the in-container contract was not used to configure the EntityManagerFactory
getDataSource
public DataSource getDataSource()
从接口复制的说明:EntityManagerFactoryInfo
Return the JDBC DataSource that this EntityManagerFactory obtains its JDBC Connections from.- 指定者:
getDataSource
在接口中EntityManagerFactoryInfo
- 返回:
- the JDBC DataSource, or
null
if not known
getObject
public EntityManagerFactory getObject()
Return the singleton EntityManagerFactory.- 指定者:
getObject
在接口中FactoryBean<EntityManagerFactory>
- 返回:
- an instance of the bean (can be
null
) - 另请参阅:
FactoryBeanNotInitializedException
getObjectType
public Class<? extends EntityManagerFactory> getObjectType()
从接口复制的说明:FactoryBean
Return the type of object that this FactoryBean creates, ornull
if not known in advance.This allows one to check for specific types of beans without instantiating objects, for example on autowiring.
In the case of implementations that are creating a singleton object, this method should try to avoid singleton creation as far as possible; it should rather estimate the type in advance. For prototypes, returning a meaningful type here is advisable too.
This method can be called before this FactoryBean has been fully initialized. It must not rely on state created during initialization; of course, it can still use such state if available.
NOTE: Autowiring will simply ignore FactoryBeans that return
null
here. Therefore it is highly recommended to implement this method properly, using the current state of the FactoryBean.- 指定者:
getObjectType
在接口中FactoryBean<EntityManagerFactory>
- 返回:
- the type of object that this FactoryBean creates, or
null
if not known at the time of the call - 另请参阅:
ListableBeanFactory.getBeansOfType(java.lang.Class<T>)
isSingleton
public boolean isSingleton()
从接口复制的说明:FactoryBean
Is the object managed by this factory a singleton? That is, willFactoryBean.getObject()
always return the same object (a reference that can be cached)?NOTE: If a FactoryBean indicates to hold a singleton object, the object returned from
getObject()
might get cached by the owning BeanFactory. Hence, do not returntrue
unless the FactoryBean always exposes the same reference.The singleton status of the FactoryBean itself will generally be provided by the owning BeanFactory; usually, it has to be defined as singleton there.
NOTE: This method returning
false
does not necessarily indicate that returned objects are independent instances. An implementation of the extendedSmartFactoryBean
interface may explicitly indicate independent instances through itsSmartFactoryBean.isPrototype()
method. PlainFactoryBean
implementations which do not implement this extended interface are simply assumed to always return independent instances if theisSingleton()
implementation returnsfalse
.- 指定者:
isSingleton
在接口中FactoryBean<EntityManagerFactory>
- 返回:
- whether the exposed object is a singleton
- 另请参阅:
FactoryBean.getObject()
,SmartFactoryBean.isPrototype()
destroy
public void destroy()
Close the EntityManagerFactory on bean factory shutdown.- 指定者:
destroy
在接口中DisposableBean
writeReplace
protected Object writeReplace() throws ObjectStreamException