Persistence.xml在右侧文件夹中,但我仍然收到错误

时间:2017-03-14 01:21:01

标签: java hibernate jpa gradle persistence.xml

我正在使用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'
}

和我的persistence.xml(位于src/main/resources/META-INF)看起来像:

<?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>

我正在解决一个难题:

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)

这就是堆栈跟踪中的所有内容。

1 个答案:

答案 0 :(得分:2)

我相信你正在尝试使用:

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

但是这个类不在你的类路径上。

在使用Hibernate时,请尝试切换到:

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