public abstract class AbstractEntityManagerFactoryBean extends java.lang.Object implements org.springframework.beans.factory.FactoryBean<EntityManagerFactory>, org.springframework.beans.factory.BeanClassLoaderAware, org.springframework.beans.factory.BeanFactoryAware, org.springframework.beans.factory.BeanNameAware, org.springframework.beans.factory.InitializingBean, org.springframework.beans.factory.DisposableBean, EntityManagerFactoryInfo, org.springframework.dao.support.PersistenceExceptionTranslator, java.io.Serializable
FactoryBean
that creates
a local JPA EntityManagerFactory
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's
org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor
,
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.
LocalEntityManagerFactoryBean
,
LocalContainerEntityManagerFactoryBean
,
Serialized FormModifier and Type | Field and Description |
---|---|
protected Log |
logger
Logger available to subclasses
|
Constructor and Description |
---|
AbstractEntityManagerFactoryBean() |
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet() |
protected EntityManagerFactory |
createEntityManagerFactoryProxy(EntityManagerFactory emf)
Create a proxy of the given EntityManagerFactory.
|
protected abstract EntityManagerFactory |
createNativeEntityManagerFactory()
Subclasses must implement this method to create the EntityManagerFactory
that will be returned by the
getObject() method. |
void |
destroy()
Close the EntityManagerFactory on bean factory shutdown.
|
java.lang.ClassLoader |
getBeanClassLoader()
Return the ClassLoader that the application's beans are loaded with.
|
org.springframework.core.task.AsyncTaskExecutor |
getBootstrapExecutor()
Return the asynchronous executor for background bootstrapping, if any.
|
javax.sql.DataSource |
getDataSource()
Return the JDBC DataSource that this EntityManagerFactory
obtains its JDBC Connections from.
|
java.lang.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, or
null if not known. |
java.util.Map<java.lang.String,java.lang.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, or
null if not known. |
EntityManagerFactory |
getNativeEntityManagerFactory()
Return the raw underlying EntityManagerFactory.
|
EntityManagerFactory |
getObject()
Return the singleton EntityManagerFactory.
|
java.lang.Class<? extends EntityManagerFactory> |
getObjectType() |
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.
|
java.lang.String |
getPersistenceUnitName()
Return the name of the persistence unit used to create this
EntityManagerFactory, or
null if it is an unnamed default. |
boolean |
isSingleton() |
void |
setBeanClassLoader(java.lang.ClassLoader classLoader) |
void |
setBeanFactory(org.springframework.beans.factory.BeanFactory beanFactory) |
void |
setBeanName(java.lang.String name) |
void |
setBootstrapExecutor(org.springframework.core.task.AsyncTaskExecutor bootstrapExecutor)
Specify an asynchronous executor for background bootstrapping,
e.g.
|
void |
setEntityManagerFactoryInterface(java.lang.Class<? extends EntityManagerFactory> emfInterface)
Specify the (potentially vendor-specific) EntityManagerFactory interface
that this EntityManagerFactory proxy is supposed to implement.
|
void |
setEntityManagerInterface(java.lang.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(java.util.Properties jpaProperties)
Specify JPA properties, to be passed into
Persistence.createEntityManagerFactory (if any). |
void |
setJpaPropertyMap(java.util.Map<java.lang.String,?> jpaProperties)
Specify JPA properties as a Map, to be passed into
Persistence.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(java.lang.Class<? extends PersistenceProvider> persistenceProviderClass)
Set the PersistenceProvider implementation class to use for creating the
EntityManagerFactory.
|
void |
setPersistenceUnitName(java.lang.String persistenceUnitName)
Specify the name of the EntityManagerFactory configuration.
|
org.springframework.dao.DataAccessException |
translateExceptionIfPossible(java.lang.RuntimeException ex)
Implementation of the PersistenceExceptionTranslator interface, as
autodetected by Spring's PersistenceExceptionTranslationPostProcessor.
|
protected java.lang.Object |
writeReplace() |
protected final Log logger
public void setPersistenceProviderClass(java.lang.Class<? extends PersistenceProvider> persistenceProviderClass)
public void setPersistenceProvider(@Nullable PersistenceProvider persistenceProvider)
@Nullable public PersistenceProvider getPersistenceProvider()
EntityManagerFactoryInfo
getPersistenceProvider
in interface EntityManagerFactoryInfo
null
if the standard JPA provider autodetection process
was used to configure the EntityManagerFactorypublic void setPersistenceUnitName(@Nullable java.lang.String persistenceUnitName)
Default is none, indicating the default EntityManagerFactory configuration. The persistence provider will throw an exception if ambiguous EntityManager configurations are found.
@Nullable public java.lang.String getPersistenceUnitName()
EntityManagerFactoryInfo
null
if it is an unnamed default.
If getPersistenceUnitInfo()
returns non-null, the result of
getPersistenceUnitName()
must be equal to the value returned by
PersistenceUnitInfo.getPersistenceUnitName()
.
getPersistenceUnitName
in interface EntityManagerFactoryInfo
EntityManagerFactoryInfo.getPersistenceUnitInfo()
,
PersistenceUnitInfo.getPersistenceUnitName()
public void setJpaProperties(java.util.Properties jpaProperties)
Persistence.createEntityManagerFactory
(if any).
Can be populated with a String "value" (parsed via PropertiesEditor) or a "props" element in XML bean definitions.
public void setJpaPropertyMap(@Nullable java.util.Map<java.lang.String,?> jpaProperties)
Persistence.createEntityManagerFactory
(if any).
Can be populated with a "map" or "props" element in XML bean definitions.
public java.util.Map<java.lang.String,java.lang.Object> getJpaPropertyMap()
Useful for specifying entries directly, for example via "jpaPropertyMap[myKey]".
public void setEntityManagerFactoryInterface(java.lang.Class<? extends EntityManagerFactory> emfInterface)
The default will be taken from the specific JpaVendorAdapter, if any,
or set to the standard javax.persistence.EntityManagerFactory
interface else.
public void setEntityManagerInterface(@Nullable java.lang.Class<? extends EntityManager> emInterface)
The default will be taken from the specific JpaVendorAdapter, if any,
or set to the standard javax.persistence.EntityManager
interface else.
@Nullable public java.lang.Class<? extends EntityManager> getEntityManagerInterface()
EntityManagerFactoryInfo
A null
return value suggests that autodetection is supposed
to happen: either based on a target EntityManager
instance
or simply defaulting to javax.persistence.EntityManager
.
getEntityManagerInterface
in interface EntityManagerFactoryInfo
public void setJpaDialect(@Nullable JpaDialect jpaDialect)
EntityManagerFactoryInfo.getJpaDialect()
@Nullable public JpaDialect getJpaDialect()
EntityManagerFactoryInfo
null
if not known.getJpaDialect
in interface EntityManagerFactoryInfo
public void setJpaVendorAdapter(@Nullable JpaVendorAdapter jpaVendorAdapter)
@Nullable public JpaVendorAdapter getJpaVendorAdapter()
null
if not known.public void setBootstrapExecutor(@Nullable org.springframework.core.task.AsyncTaskExecutor bootstrapExecutor)
SimpleAsyncTaskExecutor
.
EntityManagerFactory
initialization will then switch into background
bootstrap mode, with a EntityManagerFactory
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 a EntityManagerFactory
method will
then block until the JPA provider's bootstrapping completed, if not ready by then.
For maximum benefit, make sure to avoid early EntityManagerFactory
calls
in init methods of related beans, even for metadata introspection purposes.
@Nullable public org.springframework.core.task.AsyncTaskExecutor getBootstrapExecutor()
public void setBeanClassLoader(java.lang.ClassLoader classLoader)
setBeanClassLoader
in interface org.springframework.beans.factory.BeanClassLoaderAware
public java.lang.ClassLoader getBeanClassLoader()
EntityManagerFactoryInfo
Proxies will be generated in this ClassLoader.
getBeanClassLoader
in interface EntityManagerFactoryInfo
public void setBeanFactory(org.springframework.beans.factory.BeanFactory beanFactory)
setBeanFactory
in interface org.springframework.beans.factory.BeanFactoryAware
public void setBeanName(java.lang.String name)
setBeanName
in interface org.springframework.beans.factory.BeanNameAware
public final void afterPropertiesSet() throws PersistenceException
afterPropertiesSet
in interface org.springframework.beans.factory.InitializingBean
PersistenceException
protected EntityManagerFactory createEntityManagerFactoryProxy(@Nullable EntityManagerFactory emf)
emf
- EntityManagerFactory as returned by the persistence provider,
if initialized alreadyprotected abstract EntityManagerFactory createNativeEntityManagerFactory() throws PersistenceException
getObject()
method.PersistenceException
- if the EntityManager cannot be created@Nullable public org.springframework.dao.DataAccessException translateExceptionIfPossible(java.lang.RuntimeException ex)
Uses the dialect's conversion if possible; otherwise falls back to standard JPA exception conversion.
translateExceptionIfPossible
in interface org.springframework.dao.support.PersistenceExceptionTranslator
org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor
,
PersistenceExceptionTranslator.translateExceptionIfPossible(java.lang.RuntimeException)
,
EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(java.lang.RuntimeException)
public EntityManagerFactory getNativeEntityManagerFactory()
EntityManagerFactoryInfo
getNativeEntityManagerFactory
in interface EntityManagerFactoryInfo
null
)@Nullable public PersistenceUnitInfo getPersistenceUnitInfo()
EntityManagerFactoryInfo
getPersistenceUnitInfo
in interface EntityManagerFactoryInfo
null
if the in-container contract was not used to
configure the EntityManagerFactory@Nullable public javax.sql.DataSource getDataSource()
EntityManagerFactoryInfo
getDataSource
in interface EntityManagerFactoryInfo
null
if not known@Nullable public EntityManagerFactory getObject()
getObject
in interface org.springframework.beans.factory.FactoryBean<EntityManagerFactory>
public java.lang.Class<? extends EntityManagerFactory> getObjectType()
getObjectType
in interface org.springframework.beans.factory.FactoryBean<EntityManagerFactory>
public boolean isSingleton()
isSingleton
in interface org.springframework.beans.factory.FactoryBean<EntityManagerFactory>
public void destroy()
destroy
in interface org.springframework.beans.factory.DisposableBean
protected java.lang.Object writeReplace() throws java.io.ObjectStreamException
java.io.ObjectStreamException