public abstract class AbstractEntityManagerFactoryBean extends Object implements FactoryBean<EntityManagerFactory>, BeanClassLoaderAware, BeanFactoryAware, BeanNameAware, InitializingBean, DisposableBean, EntityManagerFactoryInfo, PersistenceExceptionTranslator, 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 as well as
Spring's JpaVendorAdapter
abstraction, and controls the
EntityManagerFactory's lifecycle.
This class also implements the
PersistenceExceptionTranslator
interface, as autodetected by Spring's
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
|
EntityManagerFactory |
nativeEntityManagerFactory
Raw EntityManagerFactory as returned by the PersistenceProvider
|
Constructor and Description |
---|
AbstractEntityManagerFactoryBean() |
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet()
Invoked by a BeanFactory after it has set all bean properties supplied
(and satisfied BeanFactoryAware and ApplicationContextAware).
|
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.
|
ClassLoader |
getBeanClassLoader()
Return the ClassLoader that the application's beans are loaded with.
|
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, or
null 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, or
null 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,
or
null 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, or
null if it is an unnamed default. |
boolean |
isSingleton()
Is the object managed by this factory a singleton? That is,
will
FactoryBean.getObject() always return the same object
(a reference that can be cached)? |
void |
setBeanClassLoader(ClassLoader classLoader)
Callback that supplies the bean
class 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 |
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 into
Persistence.createEntityManagerFactory (if any). |
void |
setJpaPropertyMap(Map<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(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() |
protected final Log logger
public EntityManagerFactory nativeEntityManagerFactory
public void setPersistenceProviderClass(Class<? extends PersistenceProvider> persistenceProviderClass)
public void setPersistenceProvider(PersistenceProvider persistenceProvider)
public PersistenceProvider getPersistenceProvider()
EntityManagerFactoryInfo
getPersistenceProvider
in interface EntityManagerFactoryInfo
null
if the standard JPA provider autodetection process
was used to configure the EntityManagerFactorypublic void setPersistenceUnitName(String persistenceUnitName)
Default is none, indicating the default EntityManagerFactory configuration. The persistence provider will throw an exception if ambiguous EntityManager configurations are found.
public 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(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(Map<String,?> jpaProperties)
Persistence.createEntityManagerFactory
(if any).
Can be populated with a "map" or "props" element in XML bean definitions.
public Map<String,Object> getJpaPropertyMap()
Useful for specifying entries directly, for example via "jpaPropertyMap[myKey]".
public void setEntityManagerFactoryInterface(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(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.
public 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(JpaDialect jpaDialect)
EntityManagerFactoryInfo.getJpaDialect()
public JpaDialect getJpaDialect()
EntityManagerFactoryInfo
null
if not known.getJpaDialect
in interface EntityManagerFactoryInfo
public void setJpaVendorAdapter(JpaVendorAdapter jpaVendorAdapter)
public JpaVendorAdapter getJpaVendorAdapter()
null
if not known.public void setBeanClassLoader(ClassLoader classLoader)
BeanClassLoaderAware
class 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
in interface BeanClassLoaderAware
classLoader
- the owning class loader; may be null
in
which case a default ClassLoader
must be used, for example
the ClassLoader
obtained via
ClassUtils.getDefaultClassLoader()
public ClassLoader getBeanClassLoader()
EntityManagerFactoryInfo
Proxies will be generated in this ClassLoader.
getBeanClassLoader
in interface EntityManagerFactoryInfo
public void setBeanFactory(BeanFactory beanFactory)
BeanFactoryAware
Invoked after the population of normal bean properties
but before an initialization callback such as
InitializingBean.afterPropertiesSet()
or a custom init-method.
setBeanFactory
in interface BeanFactoryAware
beanFactory
- owning BeanFactory (never null
).
The bean can immediately call methods on the factory.BeanInitializationException
public void setBeanName(String name)
BeanNameAware
Invoked after population of normal bean properties but before an
init callback such as InitializingBean.afterPropertiesSet()
or a custom init-method.
setBeanName
in interface 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 the BeanFactoryUtils.originalBeanName(String)
method to extract the original bean name (without suffix), if desired.public final void afterPropertiesSet() throws PersistenceException
InitializingBean
This method allows the bean instance to perform initialization only possible when all bean properties have been set and to throw an exception in the event of misconfiguration.
afterPropertiesSet
in interface InitializingBean
PersistenceException
protected EntityManagerFactory createEntityManagerFactoryProxy(EntityManagerFactory emf)
emf
- EntityManagerFactory as returned by the persistence providerprotected abstract EntityManagerFactory createNativeEntityManagerFactory() throws PersistenceException
getObject()
method.PersistenceException
- if the EntityManager cannot be createdpublic DataAccessException translateExceptionIfPossible(RuntimeException ex)
Uses the dialect's conversion if possible; otherwise falls back to standard JPA exception conversion.
translateExceptionIfPossible
in interface PersistenceExceptionTranslator
ex
- a RuntimeException to translatenull
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)
public EntityManagerFactory getNativeEntityManagerFactory()
EntityManagerFactoryInfo
getNativeEntityManagerFactory
in interface EntityManagerFactoryInfo
null
)public PersistenceUnitInfo getPersistenceUnitInfo()
EntityManagerFactoryInfo
getPersistenceUnitInfo
in interface EntityManagerFactoryInfo
null
if the in-container contract was not used to
configure the EntityManagerFactorypublic DataSource getDataSource()
EntityManagerFactoryInfo
getDataSource
in interface EntityManagerFactoryInfo
null
if not knownpublic EntityManagerFactory getObject()
getObject
in interface FactoryBean<EntityManagerFactory>
null
)FactoryBeanNotInitializedException
public Class<? extends EntityManagerFactory> getObjectType()
FactoryBean
null
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
in interface FactoryBean<EntityManagerFactory>
null
if not known at the time of the callListableBeanFactory.getBeansOfType(java.lang.Class<T>)
public boolean isSingleton()
FactoryBean
FactoryBean.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 return true
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 extended SmartFactoryBean
interface
may explicitly indicate independent instances through its
SmartFactoryBean.isPrototype()
method. Plain FactoryBean
implementations which do not implement this extended interface are
simply assumed to always return independent instances if the
isSingleton()
implementation returns false
.
isSingleton
in interface FactoryBean<EntityManagerFactory>
FactoryBean.getObject()
,
SmartFactoryBean.isPrototype()
public void destroy()
destroy
in interface DisposableBean
protected Object writeReplace() throws ObjectStreamException
ObjectStreamException