W3docs

No Persistence provider for EntityManager named

If you are seeing the error "No Persistence provider for EntityManager named [persistence-unit-name]" in your Java application, it means that the persistence provider (e.g.

If you are seeing the error "No Persistence provider for EntityManager named [persistence-unit-name]" in your Java application, it means that the persistence provider (e.g. Hibernate) could not be found for the specified persistence unit.

There are a few possible causes for this error:

  1. The persistence provider dependency is missing: Make sure that you have added the dependency for the persistence provider (e.g. Hibernate) in your project. Maven:
    `<dependency>`
        `<groupId>`org.hibernate.orm`</groupId>`
        `<artifactId>`hibernate-core`</artifactId>`
        `<version>`6.4.0.Final`</version>`
    `</dependency>`
    Gradle:
    implementation 'org.hibernate.orm:hibernate-core:6.4.0.Final'
  2. The persistence unit name is incorrect: Make sure that you are using the correct persistence unit name in your code. The name should match the name specified in the persistence.xml file.
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("myPersistenceUnit");
  3. The persistence.xml file is not on the classpath: Make sure that the persistence.xml file is on the classpath (typically src/main/resources/META-INF/persistence.xml) and that it is configured correctly.
  4. The persistence.xml file is invalid: Check the persistence.xml file for any syntax errors or invalid configuration. Ensure you are using the correct namespace for your environment:
    <?xml version="1.0" encoding="UTF-8"?>
    `<persistence xmlns="https://jakarta.ee/xml/ns/persistence" version="3.1">`
        `<persistence-unit name="myPersistenceUnit" transaction-type="RESOURCE_LOCAL">`
            `<provider>`org.hibernate.jpa.HibernatePersistenceProvider`</provider>`
            `<class>`com.example.MyEntity`</class>`
            `<properties>`
                `<property name="jakarta.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/mydb"/>`
                `<property name="jakarta.persistence.jdbc.user" value="root"/>`
                `<property name="jakarta.persistence.jdbc.password" value="password"/>`
            `</properties>`
        `</persistence-unit>`
    `</persistence>`
    (Note: For legacy Java EE 8 or older, use the javax.persistence namespace and hibernate.* property prefixes.)

To fix this error, you will need to identify and correct the cause of the problem. If you are using a dependency management tool such as Maven or Gradle, make sure that the correct dependencies are being added to the project. If you are using Hibernate, you will also need to configure the persistence.xml file with the correct settings for your database.