JPA
EntityManager
Persistence provider
Java
ORM

No Persistence provider for EntityManager named

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Introduction

No Persistence provider for EntityManager named ... means JPA could not find a configured persistence unit with that name, or it could not load a provider that knows how to create it. In plain terms, your code asked for an EntityManagerFactory, but the runtime could not match that request to a valid JPA configuration.

What JPA Is Looking For

When you call Persistence.createEntityManagerFactory("myUnit"), JPA searches for a META-INF/persistence.xml file on the classpath. Inside that file, it expects a persistence-unit element whose name matches the string you passed.

It also needs an actual provider implementation, such as Hibernate or EclipseLink, available at runtime. The JPA API alone is not enough.

A minimal example looks like this.

java
1import jakarta.persistence.EntityManager;
2import jakarta.persistence.EntityManagerFactory;
3import jakarta.persistence.Persistence;
4
5public class Main {
6    public static void main(String[] args) {
7        EntityManagerFactory emf = Persistence.createEntityManagerFactory("myUnit");
8        EntityManager em = emf.createEntityManager();
9        em.close();
10        emf.close();
11    }
12}

If myUnit is not defined correctly, the exception appears during factory creation.

The Most Common Causes

Persistence Unit Name Mismatch

This is the simplest cause. The name in Java must exactly match the one in persistence.xml.

xml
1<persistence xmlns="https://jakarta.ee/xml/ns/persistence" version="3.0">
2  <persistence-unit name="myUnit">
3    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
4  </persistence-unit>
5</persistence>

If your code asks for myPersistenceUnit while the XML defines myUnit, JPA will fail.

persistence.xml in the Wrong Location

The file must end up at META-INF/persistence.xml on the runtime classpath. Putting it under the wrong source folder or packaging it incorrectly in the final artifact is a common problem.

In a standard Maven project, place it under src/main/resources/META-INF/persistence.xml.

Missing Provider Dependency

If the JPA API is present but Hibernate or another provider is missing, JPA has no implementation to boot.

For example, a Maven setup with Hibernate and H2 might look like this:

xml
1<dependencies>
2  <dependency>
3    <groupId>org.hibernate.orm</groupId>
4    <artifactId>hibernate-core</artifactId>
5    <version>6.4.4.Final</version>
6  </dependency>
7  <dependency>
8    <groupId>com.h2database</groupId>
9    <artifactId>h2</artifactId>
10    <version>2.2.224</version>
11    <scope>runtime</scope>
12  </dependency>
13</dependencies>

Mixing Framework Conventions

Spring Boot projects often rely on Spring Data JPA and auto-configuration rather than manual Persistence.createEntityManagerFactory(...) calls. If you copy plain JPA examples into a Spring Boot app without understanding the bootstrap model, configuration can become inconsistent.

A Working Minimal Configuration

xml
1<persistence xmlns="https://jakarta.ee/xml/ns/persistence" version="3.0">
2  <persistence-unit name="myUnit" transaction-type="RESOURCE_LOCAL">
3    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
4    <properties>
5      <property name="jakarta.persistence.jdbc.driver" value="org.h2.Driver"/>
6      <property name="jakarta.persistence.jdbc.url" value="jdbc:h2:mem:testdb"/>
7      <property name="jakarta.persistence.jdbc.user" value="sa"/>
8      <property name="jakarta.persistence.jdbc.password" value=""/>
9      <property name="hibernate.hbm2ddl.auto" value="update"/>
10    </properties>
11  </persistence-unit>
12</persistence>

With that file in the correct location and the provider jars installed, Persistence.createEntityManagerFactory("myUnit") should succeed.

A Practical Debugging Checklist

Use a strict checklist instead of guessing:

  1. confirm the exact persistence unit name used in code
  2. confirm persistence.xml is packaged under META-INF
  3. confirm a provider dependency exists at runtime
  4. confirm the XML namespace and property names match your JPA generation
  5. check startup logs for classpath or provider-loading errors

The fourth point matters because older javax.persistence examples and newer jakarta.persistence examples are not interchangeable without considering dependency versions.

Common Pitfalls

The most common pitfall is thinking the JPA API dependency is the provider. It is only the interface layer. You still need Hibernate, EclipseLink, or another implementation.

Another common mistake is copying a persistence.xml file into the source tree but not into src/main/resources/META-INF, so it never reaches the runtime classpath.

Developers also get trapped by name mismatches that look harmless, such as MyUnit versus myUnit. JPA name matching is literal.

Summary

  • JPA must find both a matching persistence-unit name and a provider implementation.
  • Put persistence.xml at src/main/resources/META-INF/persistence.xml.
  • Make sure the unit name in Java exactly matches the XML configuration.
  • Include a real provider such as Hibernate, not just the JPA API.
  • Watch for javax.persistence versus jakarta.persistence version mismatches.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.