beginnerHibernate Architecture — SessionFactory, Session, EntityManager

What's the difference between EntityManagerFactory and EntityManager, and why does that difference matter?

EntityManagerFactory is heavyweight, thread-safe, and created exactly once per application/datasource — it holds expensive parsed mappings and connection pool configuration. EntityManager is lightweight, NOT thread-safe, and created fresh per unit of work. The difference matters because recreating the factory per request is a severe, avoidable performance mistake, and sharing an EntityManager across threads causes real concurrency bugs, since it's explicitly not designed for that.

This is a Pro chapter

Sign in, then upgrade to Pro or Power to unlock this and the full Spring Ecosystem Mastery library.

What's the difference between EntityManagerFactory and EntityManager, and why does that difference matter?

Next Step

Continue to Why doesn't Spring Boot make you manually create an EntityManager for every request?← Back to all Spring Data JPA & Hibernate Mastery questions