javax.persistence.PersistenceException:EntityManager没有持久性提供程序

时间:2018-11-01 20:23:45

标签: java jpa gradle eclipselink

我正在尝试使用EclipseLink作为ORM和Gradle在Jersey上建立一个测试REST项目。

当我想测试ORM功能时,遇到以下异常:

Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named TestPU
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:85)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at com.resttest.SeedTest.main(SeedTest.java:13)

运行以下代码:

package com.resttest;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

import com.resttest.entity.User;

public class SeedTest
{
public static void main(String[] args)
{
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("TestPU");
    EntityManager em = factory.createEntityManager();

    em.getTransaction().begin();

    User user = new User();
    user.setEmail("Joe");
    user.setPassword("xxx");

    em.persist(user);
    em.getTransaction().commit();

    em.close();
}
}

我的build.gradle文件如下:

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'war'

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    compile 'javax.ws.rs:jsr311-api:1.1.1'

    compile 'com.sun.jersey:jersey-server:1.19.4'
    compile 'com.sun.jersey:jersey-core:1.19.4'
    compile 'com.sun.jersey:jersey-servlet:1.19.4'

    compile 'mysql:mysql-connector-java:5.1.47'
    compile 'org.eclipse.persistence:eclipselink:2.5.1'
}

这是我的persistence.xml:

<?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="TestPU" transaction-type="RESOURCE_LOCAL">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
        <property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/test" />
        <property name="javax.persistence.jdbc.user" value="root" />
        <property name="javax.persistence.jdbc.password" value="root" />
    </properties>
</persistence-unit>

回答这个问题的人说,您必须将JPA Provider库添加到类路径中(尽管总是在Hibernate中被询问),但是EclipseLink已经添加到我的类路径中,因此不能是那样。有什么问题吗?

1 个答案:

答案 0 :(得分:0)

好的,这很令人尴尬,但是显然我的源文件夹设置之一在Eclipse中丢失了,这就是为什么我的persistence.xml文件未被识别的原因。