gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 06:29:31 26 4
gpt4 key购买 nike

我正在尝试使用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已经添加到我的类路径中,因此不能是那样。问题是什么?

最佳答案

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

关于java - javax.persistence.PersistenceException:EntityManager没有持久性提供程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53108845/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com