1

我试图让 Hibernate 与我的 Gradle 项目一起工作,但我得到的只是线程“main”中的异常 javax.persistence.PersistenceException: No Persistence provider

for EntityManager named manago
    at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69)
    at jpa.EntityManagerFactory.reinit(EntityManagerFactory.java:69)
    at jpa.EntityManagerFactory.<init>(EntityManagerFactory.java:61)
    at jpa.EntityManagerFactory.<init>(EntityManagerFactory.java:56)
    at jpa.EntityManagerFactory.getInstance(EntityManagerFactory.java:43)
    at main.Main.main(Main.java:32)
:run FAILED

我的 build.gradle 文件:

apply plugin: 'application'
apply plugin: 'java'
apply plugin: 'war'
mainClassName = "main.Main"
sourceSets.main.output.resourcesDir = sourceSets.main.output.classesDir
sourceCompatibility = '1.6'
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'

if (!hasProperty('mainClass')) {
    ext.mainClass = 'main.Main'
}

repositories {
    mavenCentral()
}

dependencies {
    compile group: 'org.hibernate', name: 'hibernate-core', version: '3.6.7.Final'
    compile group: 'org.hibernate', name: 'hibernate-entitymanager', version: '3.6.7.Final'
    compile 'postgresql:postgresql:9.1-901-1.jdbc4'
    compile "javax.ws.rs:jsr311-api:1.1.1"
    compile 'com.sun.jersey:jersey-server:1.13'
    compile 'com.sun.jersey:jersey-core:1.13'
    compile 'com.sun.jersey:jersey-servlet:1.13'
    testCompile group: 'junit', name: 'junit', version: '4.10'
    runtime group: 'org.hibernate', name: 'hibernate-core', version: '3.6.7.Final'
    runtime group: 'org.hibernate', name: 'hibernate-entitymanager', version: '3.6.7.Final'
}

/src/META-INF 目录中的 persistence.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" 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">
<persistence-unit name="WebSISMSPUpgsql" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>jpa.Routes</class>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
        <property name="hibernate.cache.provider_class" value="org.hibernate.cache.NoCacheProvider"/>
        <property name="hibernate.connection.username" value="postgres"/>
        <property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
        <property name="hibernate.connection.password" value="postgres"/>
        <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/postgres"/>
        <property name="hibernate.ejb.event.post-insert" value="org.hibernate.ejb.event.EJB3PostInsertEventListener,org.hibernate.envers.event.AuditEventListener"/>
        <property name="hibernate.ejb.event.post-update" value="org.hibernate.ejb.event.EJB3PostUpdateEventListener,org.hibernate.envers.event.AuditEventListener"/>
        <property name="hibernate.ejb.event.post-delete" value="org.hibernate.ejb.event.EJB3PostDeleteEventListener,org.hibernate.envers.event.AuditEventListener"/>
        <property name="hibernate.ejb.event.pre-collection-update" value="org.hibernate.envers.event.AuditEventListener"/>
        <property name="hibernate.ejb.event.pre-collection-remove" value="org.hibernate.envers.event.AuditEventListener"/>
        <property name="hibernate.ejb.event.post-collection-recreate" value="org.hibernate.envers.event.AuditEventListener"/>
    </properties>
</persistence-unit>
</persistence>

主类:

package main;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;
import javax.persistence.Persistence;
import jpa.Routes;
import org.hibernate.Session;


public class Main {
   public static void main(String[] args) {
               EntityManagerFactory emf = Persistence.createEntityManagerFactory("manago");
    }
}

我应该怎么做才能最终让它工作?

4

2 回答 2

2

您的持久性单元 - 相当清楚地 - 在您的 persistence.xml 中命名为WebSISMSPUpgsql而不是manago。只需将 Main.main 更改为

public static void main(String[] args) {
 EntityManagerFactory emf = \
    Persistence.createEntityManagerFactory("WebSISMSPUpgsql");
}

或在 persistence.xml 中更改单元的名称。

于 2013-09-01T06:53:32.167 回答
0

将以下提供程序添加到您的 persistence.xml 并尝试

<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
于 2020-04-30T13:01:39.560 回答