Class LocalEntityManagerFactoryBean
- All Implemented Interfaces:
Serializable, Aware, BeanClassLoaderAware, BeanFactoryAware, BeanNameAware, DisposableBean, FactoryBean<jakarta.persistence.EntityManagerFactory>, InitializingBean, SmartFactoryBean<jakarta.persistence.EntityManagerFactory>, SmartInitializingSingleton, PersistenceExceptionTranslator, EntityManagerFactoryInfo
FactoryBean that creates a JPA
EntityManagerFactory according to JPA's standard
standalone bootstrap contract. This is the simplest way to set up a
shared JPA EntityManagerFactory in a Spring application context; the
EntityManagerFactory can then be passed to JPA-based DAOs via
dependency injection. Note that switching to a JNDI lookup or to a
LocalContainerEntityManagerFactoryBean definition based on the
JPA container contract is just a matter of configuration!
Configuration settings are usually read from a META-INF/persistence.xml
config file, residing in the class path, according to the JPA standalone bootstrap
contract. See the Java Persistence API specification and your persistence provider
documentation for setup details. Additionally, JPA properties can also be added
on this FactoryBean via AbstractEntityManagerFactoryBean.setJpaProperties(Properties)/AbstractEntityManagerFactoryBean.setJpaPropertyMap(Map).
Note: This FactoryBean has limited configuration power in terms of
the configuration that it is able to pass to the JPA provider. If you need
more flexible configuration options, consider using Spring's more powerful
LocalContainerEntityManagerFactoryBean instead.
- Since:
- 2.0
- Author:
- Juergen Hoeller, Rod Johnson
- See Also:
-
Field Summary
Fields inherited from class AbstractEntityManagerFactoryBean
loggerFields inherited from interface FactoryBean
OBJECT_TYPE_ATTRIBUTE -
Constructor Summary
ConstructorsConstructorDescriptionCreate aLocalEntityManagerFactoryBean.LocalEntityManagerFactoryBean(jakarta.persistence.PersistenceConfiguration configuration) Create aLocalEntityManagerFactoryBeanfor the given persistence unit.LocalEntityManagerFactoryBean(String persistenceUnitName) Create aLocalEntityManagerFactoryBeanfor the given persistence unit. -
Method Summary
Modifier and TypeMethodDescriptionprotected jakarta.persistence.EntityManagerFactoryInitialize the EntityManagerFactory for the given configuration.Expose the JDBC DataSource from the "jakarta.persistence.dataSource" property, if any.jakarta.persistence.PersistenceConfigurationSet a local JPA 3.2PersistenceConfigurationto use for creating the EntityManagerFactory.voidsetDataSource(@Nullable DataSource dataSource) Specify the JDBC DataSource that the JPA persistence provider is supposed to use for accessing the database.voidsetPersistenceConfiguration(jakarta.persistence.PersistenceConfiguration configuration) Set a local JPA 3.2PersistenceConfigurationto use for creating the EntityManagerFactory.voidsetPersistenceUnitName(@Nullable String persistenceUnitName) Specify the name of the EntityManagerFactory configuration.Methods inherited from class AbstractEntityManagerFactoryBean
afterPropertiesSet, afterSingletonsInstantiated, createEntityManagerFactoryProxy, createNativeEntityManager, destroy, getBeanClassLoader, getBeanFactory, getBeanName, getBootstrapExecutor, getEntityManagerInterface, getJpaDialect, getJpaPropertyMap, getJpaVendorAdapter, getNativeEntityManagerFactory, getObject, getObject, getObjectType, getPersistenceProvider, getPersistenceUnitInfo, getPersistenceUnitName, postProcessEntityManager, setBeanClassLoader, setBeanFactory, setBeanName, setBootstrapExecutor, setEntityManagerFactoryInterface, setEntityManagerInitializer, setEntityManagerInterface, setJpaDialect, setJpaProperties, setJpaPropertyMap, setJpaVendorAdapter, setPersistenceProvider, setPersistenceProviderClass, supportsType, translateExceptionIfPossible, writeReplaceMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface FactoryBean
isSingletonMethods inherited from interface SmartFactoryBean
isEagerInit, isPrototype
-
Constructor Details
-
LocalEntityManagerFactoryBean
public LocalEntityManagerFactoryBean()Create aLocalEntityManagerFactoryBean.As of 7.0, This uses "default" for the default persistence unit name.
- See Also:
-
LocalEntityManagerFactoryBean
Create aLocalEntityManagerFactoryBeanfor the given persistence unit.- Parameters:
persistenceUnitName- the name of the persistence unit- Since:
- 7.0
-
LocalEntityManagerFactoryBean
public LocalEntityManagerFactoryBean(jakarta.persistence.PersistenceConfiguration configuration) Create aLocalEntityManagerFactoryBeanfor the given persistence unit.- Parameters:
configuration- the configuration for the persistence unit- Since:
- 7.0
-
-
Method Details
-
setPersistenceConfiguration
public void setPersistenceConfiguration(jakarta.persistence.PersistenceConfiguration configuration) Set a local JPA 3.2PersistenceConfigurationto use for creating the EntityManagerFactory. This can be a provider-specific subclass such asHibernatePersistenceConfiguration, exposing a complete programmatic persistence unit configuration which replacespersistence.xml(including provider-specific classpath scanning).Note:
PersistenceConfigurationincludes a persistence unit name, so this effectively overrides thesetPersistenceUnitName(String)method. In contrast, locally specified JPA properties (AbstractEntityManagerFactoryBean.setJpaProperties(Properties)) will get merged into the givenPersistenceConfigurationinstance.- Since:
- 7.0
- See Also:
-
getPersistenceConfiguration
public jakarta.persistence.PersistenceConfiguration getPersistenceConfiguration()Set a local JPA 3.2PersistenceConfigurationto use for creating the EntityManagerFactory. If none is in use yet, a new plainPersistenceConfigurationfor the configured persistence unit name will be created and returned.- Since:
- 7.0
- See Also:
-
setPersistenceUnitName
Description copied from class:AbstractEntityManagerFactoryBeanSpecify 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.
- Overrides:
setPersistenceUnitNamein classAbstractEntityManagerFactoryBean- See Also:
-
setDataSource
Specify the JDBC DataSource that the JPA persistence provider is supposed to use for accessing the database. This is an alternative to keeping the JDBC configuration inpersistence.xml, passing in a Spring-managed DataSource through the "jakarta.persistence.dataSource" property instead.When configured here, the JDBC DataSource will also get autodetected by
JpaTransactionManagerfor exposing JPA transactions to JDBC accessors.- Since:
- 6.2
- See Also:
-
getDataSource
Expose the JDBC DataSource from the "jakarta.persistence.dataSource" property, if any.- Specified by:
getDataSourcein interfaceEntityManagerFactoryInfo- Overrides:
getDataSourcein classAbstractEntityManagerFactoryBean- Returns:
- the JDBC DataSource, or
nullif not known - Since:
- 6.2
- See Also:
-
createNativeEntityManagerFactory
protected jakarta.persistence.EntityManagerFactory createNativeEntityManagerFactory() throws jakarta.persistence.PersistenceExceptionInitialize the EntityManagerFactory for the given configuration.- Specified by:
createNativeEntityManagerFactoryin classAbstractEntityManagerFactoryBean- Returns:
- the EntityManagerFactory instance returned by this FactoryBean
- Throws:
jakarta.persistence.PersistenceException- in case of JPA initialization errors
-