Filipe Andrade
Filipe Andrade

Reputation: 69

Persistence.xml in the right folder, but I still get an error

I am configuring from scratch a new project using Gradle:

apply plugin: 'java-library'

repositories {
    mavenCentral()
}

dependencies {
    compile 'org.hsqldb:hsqldb:2.3.4'
    compile 'javax.persistence:persistence-api:1.0.2'
    compile 'org.hibernate:hibernate-core:5.2.8.Final'
    compile 'org.hibernate:hibernate-entitymanager:5.2.8.Final'

    api 'org.apache.commons:commons-math3:3.6.1'

    implementation 'com.google.guava:guava:20.0'

    testImplementation 'junit:junit:4.12'
}

and my persistence.xml (located at src/main/resources/META-INF) looks like:

<?xml version="1.0" encoding="UTF-8" ?>
<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" xmlns="http://java.sun.com/xml/ns/persistence">

    <persistence-unit name="app" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>com.fsa.hibernate.model.User</class>


        <properties>
            <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:testdb" />
            <property name="javax.persistence.jdbc.user" value="sa" />
            <property name="javax.persistence.jdbc.password" value="" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
        </properties>
    </persistence-unit>

</persistence>

And I am getting a hard problem to solve:

Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named app:  The following providers:
org.hibernate.jpa.HibernatePersistenceProvider
Returned null to createEntityManagerFactory.

    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:154)
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:83)
    at com.fsa.hibernate.model.UserTest.main(UserTest.java:15)

And that is all the content in the stack trace.

Upvotes: 1

Views: 625

Answers (1)

MartinTeeVarga
MartinTeeVarga

Reputation: 10898

I believe you are trying to use:

<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>

but this class is not on your classpath.

As you are using Hibernate, try switching to:

<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>

Upvotes: 2

Related Questions