简体   繁体   中英

javax.persistence.PersistenceException: No Persistence provider for EntityManager

I am trying to set up a test REST project with Jersey using EclipseLink as ORM and Gradle.

When i wanted to test the ORM functionality i encountered the following exception:

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)

Running the following code:

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

My build.gradle file looks like this:

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'
}

This is my 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>

Answers to this question said you have to add the JPA Provider library to the classpath (although it was always asked with Hibernate) but EclipseLink is already added to my classpath so it cannot be that. What is the problem?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM