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.

Ready to master this question?

Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.

Sign in to generate a response

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