LocalContainerEntityManagerFactoryBean is Spring's factory bean that builds a JPA EntityManagerFactory using the container-managed bootstrap contract (PersistenceUnitInfo). You give it a DataSource, a JPA vendor adapter such as Hibernate, the packages that contain your entities and optional provider properties, and it produces the EntityManagerFactory that the rest of your persistence layer uses.
In a Spring Boot application you rarely create it yourself, because HibernateJpaAutoConfiguration does it for you. You define it explicitly for multiple data sources, custom persistence units or non-Boot Spring applications.
EntityManagerFactoryBuilderJPA has two bootstrap styles. In a Jakarta EE container the server reads persistence.xml and creates the factory. In plain Java SE you call Persistence.createEntityManagerFactory. LocalContainerEntityManagerFactoryBean gives you container-style bootstrap inside Spring, so you can:
persistence.xml entirely and scan packages for @Entity classes with setPackagesToScan.DataSource (connection pool, JNDI or test database).JpaVendorAdapter.JpaTransactionManager.EntityManager proxy with @PersistenceContext.Because it implements FactoryBean<EntityManagerFactory>, you inject EntityManagerFactory (or EntityManager), not the factory bean itself.
This example targets Spring Framework 6 and Spring Boot 3, which use the jakarta.persistence namespace:
The bean name entityManagerFactory matters: Spring Data JPA looks for a bean with that name by default. Pass a different name through @EnableJpaRepositories(entityManagerFactoryRef = "...") when you define several.
Older tutorials set hibernate.dialect to values such as org.hibernate.dialect.MySQL5Dialect. Hibernate 6 removed the version-specific dialects and detects the database from the JDBC connection, so you can normally omit the property. If you must set it, use the current names, for example org.hibernate.dialect.MySQLDialect or org.hibernate.dialect.PostgreSQLDialect.
Boot builds the same bean from properties and does not need a configuration class:
Boot also scans for entities starting at the package of your @SpringBootApplication class. Use @EntityScan when entities live elsewhere. Prefer ddl-auto=validate plus a migration tool such as Flyway or Liquibase over update in production.
EntityManagerFactoryBuilderWith two databases, use the Boot-provided EntityManagerFactoryBuilder, which carries over your vendor adapter and properties:
Create a second bean for the other database, plus a JpaTransactionManager for each, and point each @EnableJpaRepositories at its own entityManagerFactoryRef and transactionManagerRef.
| Type | Role |
|---|---|
EntityManagerFactory | The thread-safe, expensive object that creates EntityManager instances. One per persistence unit. |
EntityManager | Per-unit-of-work object used for queries and persistence. Inject it through @PersistenceContext as a shared proxy. |
JpaVendorAdapter | Provider-specific defaults, such as HibernateJpaVendorAdapter. |
JpaTransactionManager | Binds an EntityManager to the current transaction. |
LocalEntityManagerFactoryBean | Simpler variant that requires persistence.xml and is rarely used now. |
javax.persistence imports with Spring 6 or Boot 3. They must be jakarta.persistence.JpaTransactionManager, so @Transactional does not bind the EntityManager.EntityManagerFactory per request. It should be a singleton, created once at startup.hibernate.hbm2ddl.auto=create or update enabled in production.LocalContainerEntityManagerFactoryBean in Spring Boot?Usually not. Boot auto-configures it. Define it yourself for multiple data sources or when you need full control over bootstrap.
persistence.xml?No. With setPackagesToScan Spring discovers entities itself. You only need persistence.xml if you point the bean at a persistence unit with setPersistenceUnitName.
LocalContainerEntityManagerFactoryBean and LocalEntityManagerFactoryBean?The container variant integrates with a Spring DataSource and supports package scanning. The local variant uses standalone JPA bootstrap and needs persistence.xml.
EntityManagerFactory thread-safe?Yes. The EntityManager it creates is not, which is why Spring injects a shared proxy bound to the current transaction.
LocalContainerEntityManagerFactoryBean creates and configures the JPA EntityManagerFactory inside Spring from a DataSource, a vendor adapter, packages to scan and properties. Let Spring Boot create it for typical applications, define it yourself for several data sources, use jakarta.persistence on current versions and let Hibernate detect the dialect.