【问题标题】:Using Hibernate with Gradle - no persistence provider for EntityManager named将 Hibernate 与 Gradle 一起使用 - 没有名为 EntityManager 的持久性提供程序
【发布时间】:2013-09-01 13:54:32
【问题描述】:

我试图让 Hibernate 与我的 Gradle 项目一起工作,但我得到的只是 线程“主”javax.persistence.PersistenceException 中的异常:没有持久性提供程序

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");
    }
}

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

【问题讨论】:

    标签: hibernate jpa gradle


    【解决方案1】:

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

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

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

    【讨论】:

    • 我改了但是没用
    • 什么是新的stackstrace,错误信息?我尝试了您的示例以及我的建议,它按承诺工作。
    • @pawel 这是我的 persistence.xml 所在的位置: $ find 。 -name persistence.xml => ./build/classes/main/META-INF/persistence.xml ./src/main/resources/META-INF/persistence.xml
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 2015-07-01
      • 2016-11-04
      • 2012-06-21
      • 1970-01-01
      • 2017-01-17
      • 2014-12-15
      • 1970-01-01
      • 1970-01-01
      • 2015-11-08
      相关资源
      最近更新 更多