Package org.springframework.orm.jpa

# Class LocalEntityManagerFactoryBean

java.lang.Object
org.springframework.orm.jpa.AbstractEntityManagerFactoryBean
org.springframework.orm.jpa.LocalEntityManagerFactoryBean

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`

public class LocalEntityManagerFactoryBean extends AbstractEntityManagerFactoryBean
`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(java.util.Properties)`/`AbstractEntityManagerFactoryBean.setJpaPropertyMap(java.util.Map<java.lang.String, ?>)`.

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:
  - `AbstractEntityManagerFactoryBean.setJpaProperties(java.util.Properties)`
  - `AbstractEntityManagerFactoryBean.setJpaVendorAdapter(org.springframework.orm.jpa.JpaVendorAdapter)`
  - `JpaTransactionManager.setEntityManagerFactory(jakarta.persistence.EntityManagerFactory)`
  - `LocalContainerEntityManagerFactoryBean`
  - `JndiObjectFactoryBean`
  - `SharedEntityManagerBean`
  - `Persistence.createEntityManagerFactory(java.lang.String)`
  - `PersistenceProvider.createEntityManagerFactory(java.lang.String, java.util.Map)`
  - Serialized Form

## Field Summary

### Fields inherited from class org.springframework.orm.jpa.AbstractEntityManagerFactoryBean

`logger`

### Fields inherited from interface org.springframework.beans.factory.FactoryBean

`OBJECT_TYPE_ATTRIBUTE`

## Constructor Summary

Constructors
Constructor
Description
`LocalEntityManagerFactoryBean()`

## Method Summary

Modifier and Type
Method
Description
`protected EntityManagerFactory`
`createNativeEntityManagerFactory()`
Initialize the EntityManagerFactory for the given configuration.
`DataSource`
`getDataSource()`
Expose the JDBC DataSource from the "jakarta.persistence.dataSource" property, if any.
`void`
`setDataSource(DataSource dataSource)`
Specify the JDBC DataSource that the JPA persistence provider is supposed to use for accessing the database.

### Methods inherited from class org.springframework.orm.jpa.AbstractEntityManagerFactoryBean

`afterPropertiesSet, afterSingletonsInstantiated, createEntityManagerFactoryProxy, createNativeEntityManager, destroy, getBeanClassLoader, getBootstrapExecutor, getEntityManagerInterface, getJpaDialect, getJpaPropertyMap, getJpaVendorAdapter, getNativeEntityManagerFactory, getObject, getObjectType, getPersistenceProvider, getPersistenceUnitInfo, getPersistenceUnitName, isSingleton, postProcessEntityManager, setBeanClassLoader, setBeanFactory, setBeanName, setBootstrapExecutor, setEntityManagerFactoryInterface, setEntityManagerInitializer, setEntityManagerInterface, setJpaDialect, setJpaProperties, setJpaPropertyMap, setJpaVendorAdapter, setPersistenceProvider, setPersistenceProviderClass, setPersistenceUnitName, translateExceptionIfPossible, writeReplace`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### LocalEntityManagerFactoryBean

public LocalEntityManagerFactoryBean()

## Method Details

### setDataSource

public void setDataSource(@Nullable DataSource dataSource)
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 in `persistence.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 `JpaTransactionManager` for exposing JPA transactions to JDBC accessors.

Since:
  6.2
See Also:
  - `AbstractEntityManagerFactoryBean.getJpaPropertyMap()`
  - `JpaTransactionManager.setDataSource(javax.sql.DataSource)`

### getDataSource

@Nullable public DataSource getDataSource()
Expose the JDBC DataSource from the "jakarta.persistence.dataSource" property, if any.

Specified by:
  `getDataSource` in interface `EntityManagerFactoryInfo`
Overrides:
  `getDataSource` in class `AbstractEntityManagerFactoryBean`
Returns:
  the JDBC DataSource, or `null` if not known
Since:
  6.2
See Also:
  - `AbstractEntityManagerFactoryBean.getJpaPropertyMap()`

### createNativeEntityManagerFactory

protected EntityManagerFactory createNativeEntityManagerFactory() throws PersistenceException
Initialize the EntityManagerFactory for the given configuration.

Specified by:
  `createNativeEntityManagerFactory` in class `AbstractEntityManagerFactoryBean`
Returns:
  the EntityManagerFactory instance returned by this FactoryBean
Throws:
  `PersistenceException` - in case of JPA initialization errors