EntityManagerFactory is the Jakarta Persistence (JPA) object that represents one persistence unit: your mapped entities plus one database configuration. Its main job is to create EntityManager instances, the objects you use to persist, find and query entities.
It is expensive to build, thread-safe, and meant to live for the whole application. You create one per persistence unit at startup, and you close it on shutdown.
jakarta.persistence.EntityManagerFactory (javax.persistence before Jakarta EE 9) is built from a persistence unit definition. Building it makes the provider, usually Hibernate, do the heavy work once:
DataSource access,Because of that cost, do not create one per request. In Hibernate, the EntityManagerFactory is backed by a SessionFactory, and you can get it with unwrap(SessionFactory.class).
| Aspect | EntityManagerFactory | EntityManager |
|---|---|---|
| Instances per application | One per persistence unit | Many, usually one per unit of work |
| Thread safety | Thread-safe | Not thread-safe |
| Cost | Heavy to create | Lightweight |
| Lifetime | Application lifetime | A transaction, a request or a method |
| Holds | Mappings, metamodel, shared cache | The persistence context (managed entities) |
EntityManagerFactory extends AutoCloseable since JPA 3.1, so try-with-resources works. The unit is declared in META-INF/persistence.xml:
Jakarta Persistence 3.2 adds convenience methods such as runInTransaction on the factory, which create an EntityManager, run your code in a transaction and close it for you. Check that your provider version supports 3.2 before using them.
You rarely call createEntityManagerFactory yourself in Spring. Spring Boot auto-configures a LocalContainerEntityManagerFactoryBean from your DataSource and the spring.jpa.* properties. Spring registers it as a bean, so you can inject it:
The injected EntityManager is a shared proxy: Spring gives each transaction its own real EntityManager behind it, which is why it is safe in singleton beans. Most applications use Spring Data repositories and never touch either object directly.
For several databases, define one LocalContainerEntityManagerFactoryBean per DataSource, with its own entity package, and point each repository set at its factory with @EnableJpaRepositories(entityManagerFactoryRef = "...").
createEntityManager() opens a new EntityManager, which you must close.getCriteriaBuilder() and getMetamodel() give type-safe query building and mapping information.getCache() exposes the second-level cache, for example evictAll().getPersistenceUnitUtil() checks whether an association is loaded.isOpen() and close() manage the factory. After close(), all EntityManager instances from it become unusable.No. It is costly and meant to be created once. Create the cheap EntityManager per unit of work.
Yes, it is designed to be shared across threads. The EntityManager it creates is not.
In Hibernate, SessionFactory extends EntityManagerFactory. Use the JPA interface by default and unwrap only when you need Hibernate features.
No. The Spring container closes the bean on shutdown.
The EntityManagerFactory is the long-lived, thread-safe, heavyweight factory for one persistence unit, and it produces short-lived EntityManager instances. Build it once, close it at shutdown, let Spring Boot manage it when you use Spring, and always close the EntityManager objects you create yourself.