Search code examples
javahibernatehibernate-jpa

No Persistence provider for EntityManager Error


I am getting this error "No Persistence provider for EntityManager named" and not able to proceed. I am learning hibernate and trying so hands on stuff. I tried all the methods mentions in this search forum, but still i get the same error. I tried the following

  1. SAVED THE persistent.xml file in src/main/resources/META-INF/persistent.xml.
  2. Updated the provider to "org.hibernate.jpa.HibernatePersistenceProvider".

Still no luck, can any one kindly help in how to resolve this. Below is screen shot and code.

POM:

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/org.hibernate.javax.persistence/hibernate-jpa-2.0-api -->
    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.1-api</artifactId>
        <version>1.0.0.Final</version>
    </dependency>
    
    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>6.1.0.Final</version>
    </dependency>

    
    <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.44</version>
    </dependency>
    
    
    <dependency>
        <groupId>javax.xml.bind</groupId>
        <artifactId>jaxb-api</artifactId>
        <version>2.3.1</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish.jaxb</groupId>
        <artifactId>jaxb-runtime</artifactId>
        <version>2.3.1</version>
    </dependency>
      
    
  </dependencies>

persistence.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence 
             http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
             version="2.0">
    <persistence-unit name="concretepage">
        <description>JPA Demo</description>
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
            <property name="hibernate.hbm2ddl.auto" value="update"/>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/testDB1"/>
            <property name="javax.persistence.jdbc.user" value="xxxxx"/>
            <property name="javax.persistence.jdbc.password" value="yyyyy"/>
        </properties>
    </persistence-unit>
</persistence> 

Java:

package database.hibernate;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class JPAUtility {
    private static final EntityManagerFactory emFactory;
    static {
           emFactory = Persistence.createEntityManagerFactory("concretepage");
    }
    public static EntityManager getEntityManager(){
        return emFactory.createEntityManager();
    }
    public static void close(){
        emFactory.close();
    }
} 




Java Program calling JPAUtility:


public class App2 {
    
    public static void main(String[] args) {
        
        EntityManager entityManager = JPAUtility.getEntityManager();
        entityManager.getTransaction().begin();

        ....
        entityManager.getTransaction().commit();
    }
}

Error Message:

Exception in thread "main" java.lang.ExceptionInInitializerError
    at database.hibernate.App2.main(App2.java:9)
Caused by: javax.persistence.PersistenceException: No Persistence provider for EntityManager named concretepage
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:61)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:39)
    at database.hibernate.JPAUtility.<clinit>(JPAUtility.java:9)
    ... 1 more

Project Structure: Project Structure


Solution

  • You need to change the maven dependency version from <version>6.1.0.Final</version> to <version>5.2.11.Final</version> for <artifactId>hibernate-core</artifactId> as there has been significant changes from Hibernate 5 to 6 and need to consider upgrading corresponding dependencies and schema names etc in persistance.xml file

    You can read another thread here - why is dependency to javax.persistence-api removed in hibernate-core 6.0.2

    When I tried with 6.1.0.Final - I also get the same error as mentioned below.

    enter image description here

    But when I changed version back to 5.2.11 then it connected to database.

    enter image description here

    PS : I've used the maria db instead of mysql but that should not matter.