The EntityManagerFactory is the JPA object that reads your persistence configuration, connects to the database, builds the entity metamodel and creates EntityManager instances on demand. It is expensive to create and thread-safe, so an application normally builds exactly one per persistence unit and keeps it for its whole lifetime.
An EntityManager, in contrast, is cheap, not thread-safe, and represents one persistence context (a unit of work). The factory is the bridge between the two. The examples use the jakarta.persistence package (JPA 3.x); older Java EE code uses javax.persistence.
Creating an EntityManagerFactory does the heavy work once:
META-INF/persistence.xml or from programmatic properties.DataSource.SessionFactory is the underlying implementation).EntityManager instances through createEntityManager().If a mapping is wrong, the failure appears here at startup, not at the first query.
Define the persistence unit:
Then create the factory once and an EntityManager per unit of work:
Since JPA 3.2, EntityManagerFactory and EntityManager implement AutoCloseable, so try-with-resources works as shown. On older versions call close() in a finally block. Closing the factory releases the connection pool and caches; after that you cannot create new managers.
| Aspect | EntityManagerFactory | EntityManager |
|---|---|---|
| Creation cost | High (startup scan, pool, metadata) | Low |
| Thread safety | Thread-safe | Not thread-safe |
| Lifetime | Application lifetime | One transaction or request |
| Count | One per persistence unit | Many, one per unit of work |
| Holds | Metadata, caches, connection pool | Persistence context (first-level cache) |
Create an EntityManager per request or transaction and close it afterwards. Never share one between threads.
In Spring Boot you almost never create the factory yourself. Spring configures a LocalContainerEntityManagerFactoryBean from your spring.datasource.* and spring.jpa.* properties and exposes the factory as a bean. You inject a transaction-bound EntityManager proxy instead:
The injected em is a shared proxy that resolves to the persistence context of the current transaction. If you need the factory itself, for example to read cache statistics, inject it with @PersistenceUnit:
In a Jakarta EE application server the container creates the factory from persistence.xml and you inject it the same way.
createEntityManager(Map) accepts per-manager property overrides, and getPersistenceUnitUtil() tells you whether a lazy attribute is already loaded.
EntityManagerFactory for each request. It rebuilds metadata and a connection pool every time and quickly exhausts the database.EntityManager across threads or keeping it in a singleton field outside Spring's proxy.IllegalStateException: EntityManagerFactory is closed.javax.persistence imports with a Jakarta-based provider.Yes. It is designed to be shared by all threads. The EntityManager it creates is not thread-safe.
One per persistence unit, normally one per database.
SessionFactory. Hibernate's SessionFactory extends EntityManagerFactory, and Session extends EntityManager.
No. Spring Boot auto-configures it. Inject EntityManager with @PersistenceContext or use Spring Data repositories.
EntityManagerFactory is the long-lived, thread-safe, expensive object that holds your JPA configuration and produces short-lived EntityManager instances. Build one per persistence unit, close it when the application stops, and create and close an EntityManager per unit of work. In Spring Boot and Jakarta EE the container manages this for you.