3

In Eclipse, I set up a new maven project where I want to try some database connection. So I created a User.java which is my entity and a DataBaseConnector.java where I try the db connection but I get an error:

Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named mysql-database
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:85)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at database.DataBaseConnector.getTestConnection(DataBaseConnector.java:21)
at database.DataBaseConnector.main(DataBaseConnector.java:14)

My current version of the 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="mysql-database" transaction-type="RESOURCE_LOCAL">
        <description>DataBase Connection</description>

        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <validation-mode>NONE</validation-mode>

        <properties>
            <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/sharifo" />
            <property name="javax.persistence.jdbc.user" value="admin" />
            <property name="javax.persistence.jdbc.password" value="1234" />

            <!-- <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"  /-->
            <!-- <property name="hibernate.show_sql" value="true"  /-->
            <!-- <property name="hibernate.hbm2ddl.auto" value="create" /-->
        </properties>

    </persistence-unit>

</persistence>

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>backend</groupId>
  <artifactId>shareifo</artifactId>
  <version>0.0.1</version>
  <packaging>war</packaging>

  <dependencies>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>5.3.0.Beta1</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.1-api</artifactId>
        <version>1.0.2.Final</version>
    </dependency>


     <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.8-dmr</version>
    </dependency>
  </dependencies>

</project>

I have the feeling that the persistence.xml might be mis-configured, so i uploaded my project also on github: https://github.com/ViFire/sherifo-backend.git.

I dont know how to fix or handle this error. Do you have ny tips for me?

MWiesner
  • 8,868
  • 11
  • 36
  • 70
vifire
  • 57
  • 10

1 Answers1

2

I checked out the code via the GitHub project link you provided. Two things had to be changed:

  1. Remove from pom.xml the now obsolete dependency:

    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.1-api</artifactId>
        <version>1.0.2.Final</version>
    </dependency>
    

    Note: This dependency is no longer needed and might even be confusing at runtime, as Hibernate 5.3.xyz now depends on javax.persistence:javax.persistence-api:2.2, the official JPA 2.2 spec-jar.

  2. Change the persistence.xml so it reads like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
                 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
                 http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
                 version="2.1">
        <persistence-unit name="mysql-database" transaction-type="RESOURCE_LOCAL">
    
            <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
            <validation-mode>NONE</validation-mode>
    
            <properties>
                <property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver" />
                <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/sharifo" />
                <property name="javax.persistence.jdbc.user" value="admin" />
                <property name="javax.persistence.jdbc.password" value="1234" />
    
                <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
    
                <!--
                    <property name="hibernate.show_sql" value="true"/>
                    <property name="hibernate.hbm2ddl.auto" value="create"/>
                -->
            </properties>
        </persistence-unit>
    </persistence>
    

    Note well,

    • (a) the different xmlns and JPA version (2.1) in the header,
    • (b) provider is: org.hibernate.jpa.HibernatePersistenceProvider,
    • (c) jdbc.driver is: com.mysql.cj.jdbc.Driver.

Observations

While debugging/testing, I found that the 5.3.0.Beta1 version of Hibernate seems to behave different than, e.g., the latest stable version: 5.2.17.Final. The "misleading" error message you observed will be thrown, even if everything is setup as described above.

For me this had a simple reason: In case the DB is not reachable or the credentials are incorrect, you will also encounter the message:

No Persistence provider for EntityManager named xyz...

which is - IMHO - totally confusing in this context. With Hibernate 5.2.x.Final you get an error message on the failing connection attempt, including a more precise debugging output for what to look for exactly.

For the aforementioned reason: Carefully check whether user/pw, JDBC url/hostname are correct as this might end up in a confusing error message on the non-existence of a Persistence-Provider for a Persistence-Unit that is actually available and configured correctly.

As a bonus, I will open a Pull request (PR) on your GitHub project which you can check in detail. I hope you will integrate this PR back into your project so you benefit directly.

Hope it helps.

MWiesner
  • 8,868
  • 11
  • 36
  • 70