Class AbstractEntityManagerFactoryBean
- All Implemented Interfaces:
Serializable,org.springframework.beans.factory.Aware,org.springframework.beans.factory.BeanClassLoaderAware,org.springframework.beans.factory.BeanFactoryAware,org.springframework.beans.factory.BeanNameAware,org.springframework.beans.factory.DisposableBean,org.springframework.beans.factory.FactoryBean<EntityManagerFactory>,org.springframework.beans.factory.InitializingBean,org.springframework.beans.factory.SmartInitializingSingleton,org.springframework.dao.support.PersistenceExceptionTranslator,EntityManagerFactoryInfo
- Direct Known Subclasses:
LocalContainerEntityManagerFactoryBean,LocalEntityManagerFactoryBean
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
PersistenceExceptionTranslationPostProcessor,
for AOP-based translation of native exceptions to Spring DataAccessExceptions.
Hence, the presence of, for example, LocalEntityManagerFactoryBean automatically enables
a PersistenceExceptionTranslationPostProcessor to translate JPA exceptions.
- Since:
- 2.0
- Author:
- Juergen Hoeller, Rod Johnson
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final org.apache.commons.logging.LogLogger available to subclasses.Fields inherited from interface org.springframework.beans.factory.FactoryBean
OBJECT_TYPE_ATTRIBUTE -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidvoidprotected EntityManagerFactoryCreate a proxy for the givenEntityManagerFactory.createNativeEntityManager(Map<?, ?> properties) Create a native JPA EntityManager to be used as the framework-managed resource behind an application-level EntityManager handle.protected abstract EntityManagerFactorySubclasses must implement this method to create the EntityManagerFactory that will be returned by thegetObject()method.voiddestroy()Close the EntityManagerFactory on bean factory shutdown.Return the ClassLoader that the application's beans are loaded with.org.springframework.core.task.AsyncTaskExecutorReturn the asynchronous executor for background bootstrapping, if any.Return the JDBC DataSource that this EntityManagerFactory obtains its JDBC Connections from.Class<? extends EntityManager>Return the (potentially vendor-specific) EntityManager interface that this factory's EntityManagers will implement.Return the vendor-specific JpaDialect implementation for this EntityManagerFactory, ornullif not known.AllowMapaccess to the JPA properties to be passed to the persistence provider, with the option to add or override specific entries.Return the JpaVendorAdapter implementation for this EntityManagerFactory, ornullif not known.Return the raw underlying EntityManagerFactory.Return the singleton EntityManagerFactory.Class<? extends EntityManagerFactory>Return the underlying PersistenceProvider that the underlying EntityManagerFactory was created with.Return the PersistenceUnitInfo used to create this EntityManagerFactory, if the in-container API was used.Return the name of the persistence unit used to create this EntityManagerFactory, ornullif it is an unnamed default.booleanprotected voidpostProcessEntityManager(EntityManager rawEntityManager) Optional callback for post-processing the native EntityManager before active use.voidsetBeanClassLoader(ClassLoader classLoader) voidsetBeanFactory(org.springframework.beans.factory.BeanFactory beanFactory) voidsetBeanName(String name) voidsetBootstrapExecutor(org.springframework.core.task.AsyncTaskExecutor bootstrapExecutor) Specify an asynchronous executor for background bootstrapping, for example, aSimpleAsyncTaskExecutor.voidsetEntityManagerFactoryInterface(Class<? extends EntityManagerFactory> emfInterface) Specify the (potentially vendor-specific) EntityManagerFactory interface that this EntityManagerFactory proxy is supposed to implement.voidsetEntityManagerInitializer(Consumer<EntityManager> entityManagerInitializer) Specify a callback for customizing everyEntityManagercreated by the exposedEntityManagerFactory.voidsetEntityManagerInterface(Class<? extends EntityManager> emInterface) Specify the (potentially vendor-specific) EntityManager interface that this factory's EntityManagers are supposed to implement.voidsetJpaDialect(JpaDialect jpaDialect) Specify the vendor-specific JpaDialect implementation to associate with this EntityManagerFactory.voidsetJpaProperties(Properties jpaProperties) Specify JPA properties, to be passed intoPersistence.createEntityManagerFactory(if any).voidsetJpaPropertyMap(Map<String, ?> jpaProperties) Specify JPA properties as a Map, to be passed intoPersistence.createEntityManagerFactory(if any).voidsetJpaVendorAdapter(JpaVendorAdapter jpaVendorAdapter) Specify the JpaVendorAdapter implementation for the desired JPA provider, if any.voidsetPersistenceProvider(PersistenceProvider persistenceProvider) Set the PersistenceProvider instance to use for creating the EntityManagerFactory.voidsetPersistenceProviderClass(Class<? extends PersistenceProvider> persistenceProviderClass) Set the PersistenceProvider implementation class to use for creating the EntityManagerFactory.voidsetPersistenceUnitName(String persistenceUnitName) Specify the name of the EntityManagerFactory configuration.org.springframework.dao.DataAccessExceptionImplementation of the PersistenceExceptionTranslator interface, as autodetected by Spring's PersistenceExceptionTranslationPostProcessor.protected Object
-
Field Details
-
logger
protected final org.apache.commons.logging.Log loggerLogger available to subclasses.
-
-
Constructor Details
-
AbstractEntityManagerFactoryBean
public AbstractEntityManagerFactoryBean()
-
-
Method Details
-
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
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
Description copied from interface:EntityManagerFactoryInfoReturn the underlying PersistenceProvider that the underlying EntityManagerFactory was created with.- Specified by:
getPersistenceProviderin interfaceEntityManagerFactoryInfo- Returns:
- the PersistenceProvider used to create this EntityManagerFactory,
or
nullif the standard JPA provider autodetection process was used to configure the EntityManagerFactory
-
setPersistenceUnitName
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
Description copied from interface:EntityManagerFactoryInfoReturn the name of the persistence unit used to create this EntityManagerFactory, ornullif it is an unnamed default.If
getPersistenceUnitInfo()returns non-null, the result ofgetPersistenceUnitName()must be equal to the value returned byPersistenceUnitInfo.getPersistenceUnitName().- Specified by:
getPersistenceUnitNamein interfaceEntityManagerFactoryInfo- See Also:
-
setJpaProperties
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
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
AllowMapaccess 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
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
jakarta.persistence.EntityManagerFactoryinterface else. -
setEntityManagerInterface
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
jakarta.persistence.EntityManagerinterface else. -
getEntityManagerInterface
Description copied from interface:EntityManagerFactoryInfoReturn the (potentially vendor-specific) EntityManager interface that this factory's EntityManagers will implement.A
nullreturn value suggests that autodetection is supposed to happen: either based on a targetEntityManagerinstance or simply defaulting tojakarta.persistence.EntityManager.- Specified by:
getEntityManagerInterfacein interfaceEntityManagerFactoryInfo
-
setJpaDialect
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.- See Also:
-
getJpaDialect
Description copied from interface:EntityManagerFactoryInfoReturn the vendor-specific JpaDialect implementation for this EntityManagerFactory, ornullif not known.- Specified by:
getJpaDialectin interfaceEntityManagerFactoryInfo
-
setJpaVendorAdapter
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
Return the JpaVendorAdapter implementation for this EntityManagerFactory, ornullif not known. -
setEntityManagerInitializer
Specify a callback for customizing everyEntityManagercreated by the exposedEntityManagerFactory.This is an alternative to a
JpaVendorAdapter-levelpostProcessEntityManagerimplementation, enabling convenient customizations for application purposes, for example, setting Hibernate filters. -
setBootstrapExecutor
public void setBootstrapExecutor(@Nullable org.springframework.core.task.AsyncTaskExecutor bootstrapExecutor) Specify an asynchronous executor for background bootstrapping, for example, aSimpleAsyncTaskExecutor.EntityManagerFactoryinitialization will then switch into background bootstrap mode, with aEntityManagerFactoryproxy immediately returned for injection purposes instead of waiting for the JPA provider's bootstrapping to complete. However, note that the first actual call to aEntityManagerFactorymethod will then block until the JPA provider's bootstrapping completed, if not ready by then. For maximum benefit, make sure to avoid earlyEntityManagerFactorycalls in init methods of related beans, even for metadata introspection purposes.As of 6.2, JPA initialization is enforced before context refresh completion, waiting for asynchronous bootstrapping to complete by then.
- Since:
- 4.3
-
getBootstrapExecutor
@Nullable public org.springframework.core.task.AsyncTaskExecutor getBootstrapExecutor()Return the asynchronous executor for background bootstrapping, if any.- Since:
- 4.3
-
setBeanClassLoader
- Specified by:
setBeanClassLoaderin interfaceorg.springframework.beans.factory.BeanClassLoaderAware
-
getBeanClassLoader
Description copied from interface:EntityManagerFactoryInfoReturn the ClassLoader that the application's beans are loaded with.Proxies will be generated in this ClassLoader.
- Specified by:
getBeanClassLoaderin interfaceEntityManagerFactoryInfo
-
setBeanFactory
public void setBeanFactory(org.springframework.beans.factory.BeanFactory beanFactory) - Specified by:
setBeanFactoryin interfaceorg.springframework.beans.factory.BeanFactoryAware
-
setBeanName
- Specified by:
setBeanNamein interfaceorg.springframework.beans.factory.BeanNameAware
-
afterPropertiesSet
- Specified by:
afterPropertiesSetin interfaceorg.springframework.beans.factory.InitializingBean- Throws:
PersistenceException
-
afterSingletonsInstantiated
public void afterSingletonsInstantiated()- Specified by:
afterSingletonsInstantiatedin interfaceorg.springframework.beans.factory.SmartInitializingSingleton
-
createEntityManagerFactoryProxy
Create a proxy for the givenEntityManagerFactory. We do this to be able to return a transaction-aware proxy for an application-managedEntityManager.- Parameters:
emf- the EntityManagerFactory as returned by the persistence provider, if initialized already- Returns:
- the EntityManagerFactory proxy
-
createNativeEntityManagerFactory
protected abstract EntityManagerFactory createNativeEntityManagerFactory() throws PersistenceExceptionSubclasses must implement this method to create the EntityManagerFactory that will be returned by thegetObject()method.- Returns:
- the EntityManagerFactory instance returned by this FactoryBean
- Throws:
PersistenceException- if the EntityManager cannot be created
-
translateExceptionIfPossible
@Nullable public org.springframework.dao.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.
- Specified by:
translateExceptionIfPossiblein interfaceorg.springframework.dao.support.PersistenceExceptionTranslator- See Also:
-
PersistenceExceptionTranslationPostProcessorPersistenceExceptionTranslator.translateExceptionIfPossible(java.lang.RuntimeException)EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(java.lang.RuntimeException)
-
getNativeEntityManagerFactory
Description copied from interface:EntityManagerFactoryInfoReturn the raw underlying EntityManagerFactory.- Specified by:
getNativeEntityManagerFactoryin interfaceEntityManagerFactoryInfo- Returns:
- the unadorned EntityManagerFactory (never
null)
-
createNativeEntityManager
Description copied from interface:EntityManagerFactoryInfoCreate a native JPA EntityManager to be used as the framework-managed resource behind an application-level EntityManager handle.This exposes a native
EntityManagerfrom the underlyingnative EntityManagerFactory, takingJpaVendorAdapter.postProcessEntityManager(EntityManager)into account.- Specified by:
createNativeEntityManagerin interfaceEntityManagerFactoryInfo- See Also:
-
postProcessEntityManager
Optional callback for post-processing the native EntityManager before active use.The default implementation delegates to
JpaVendorAdapter.postProcessEntityManager(jakarta.persistence.EntityManager), if available.- Parameters:
rawEntityManager- the EntityManager to post-process- Since:
- 5.3
- See Also:
-
getPersistenceUnitInfo
Description copied from interface:EntityManagerFactoryInfoReturn the PersistenceUnitInfo used to create this EntityManagerFactory, if the in-container API was used.- Specified by:
getPersistenceUnitInfoin interfaceEntityManagerFactoryInfo- Returns:
- the PersistenceUnitInfo used to create this EntityManagerFactory,
or
nullif the in-container contract was not used to configure the EntityManagerFactory
-
getDataSource
Description copied from interface:EntityManagerFactoryInfoReturn the JDBC DataSource that this EntityManagerFactory obtains its JDBC Connections from.- Specified by:
getDataSourcein interfaceEntityManagerFactoryInfo- Returns:
- the JDBC DataSource, or
nullif not known
-
getObject
Return the singleton EntityManagerFactory.- Specified by:
getObjectin interfaceorg.springframework.beans.factory.FactoryBean<EntityManagerFactory>
-
getObjectType
- Specified by:
getObjectTypein interfaceorg.springframework.beans.factory.FactoryBean<EntityManagerFactory>
-
isSingleton
public boolean isSingleton()- Specified by:
isSingletonin interfaceorg.springframework.beans.factory.FactoryBean<EntityManagerFactory>
-
destroy
public void destroy()Close the EntityManagerFactory on bean factory shutdown.- Specified by:
destroyin interfaceorg.springframework.beans.factory.DisposableBean
-
writeReplace
- Throws:
ObjectStreamException
-