gpt4 book ai didi

java - PersistenceUnitLoadingException - 创建 EntityManagerFactory 时出错

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

我花了最后几天时间寻找错误,但找不到。我不得不说我是 JPA 的新手,这是我的第一个项目,我是按照教程创建的,但有一个无法解释的错误。

我使用 Eclipse 和 Eclipselink 并连接了一个正在运行的 mySql 数据库

我的代码:

package com.tutorialspoint.eclipselink.service;

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

import com.tutorialspoint.eclipselink.entity.Employee;

public class CreateEmployee {

public static void main(String []args){
EntityManagerFactory emfactory = Persistence.createEntityManagerFactory("tutorialspoint_JPA_Eclipselink");
EntityManager entitymanager = emfactory.createEntityManager();
entitymanager.getTransaction().begin();

Employee e = new Employee();
e.setId(1201);
e.setName("Gopal");
e.setSalary(40000);
e.setDegree("Technical Manager");
entitymanager.persist(e);
entitymanager.getTransaction().commit();
entitymanager.close();
emfactory.close();
}

}

persistence.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" x xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="tutorialspoint_JPA_Eclipselink" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.tutorialspoint.eclipselink.entity.Employee</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/demo"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="removed"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver"/>
</properties>
</persistence-unit>
</persistence>

错误:

Exception in thread "main" Local Exception Stack: 
Exception [EclipseLink-30005] (Eclipse Persistence Services - 2.6.0.v20150309-bf26070): org.eclipse.persistence.exceptions.PersistenceUnitLoadingException
Exception Description: An exception was thrown while searching for persistence archives with ClassLoader: sun.misc.Launcher$AppClassLoader@1b1f1f12
Internal Exception: javax.persistence.PersistenceException: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.6.0.v20150309-bf26070): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [tutorialspoint_JPA_Eclipselink] failed.
Internal Exception: java.lang.NullPointerException
at org.eclipse.persistence.exceptions.PersistenceUnitLoadingException.exceptionSear chingForPersistenceResources(PersistenceUnitLoadingException.java:127)
at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactoryImpl(P ersistenceProvider.java:111)
at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(Persi stenceProvider.java:183)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:79)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at com.tutorialspoint.eclipselink.service.CreateEmployee.main(CreateEmployee.java:1 1)
Caused by: javax.persistence.PersistenceException: Exception [EclipseLink- 28018] (Eclipse Persistence Services - 2.6.0.v20150309-bf26070): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [tutorialspoint_JPA_Eclipselink] failed.
Internal Exception: java.lang.NullPointerException
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.createPredeployFaile dPersistenceException(EntityManagerSetupImpl.java:2023)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.predeploy(EntityMana gerSetupImpl.java:2014)
at org.eclipse.persistence.internal.jpa.deployment.JPAInitializer.callPredeploy(JPA Initializer.java:100)
at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactoryImpl(P ersistenceProvider.java:100)
... 4 more
Caused by: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.6.0.v20150309-bf26070): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [tutorialspoint_JPA_Eclipselink] failed.
Internal Exception: java.lang.NullPointerException
at org.eclipse.persistence.exceptions.EntityManagerSetupException.predeployFailed(E ntityManagerSetupException.java:231)
... 8 more
Caused by: java.lang.NullPointerException
at org.eclipse.persistence.platform.server.NoServerPlatformDetector.checkPlatform(N oServerPlatformDetector.java:28)
at org.eclipse.persistence.platform.server.ServerPlatformUtils.detectServerPlatform (ServerPlatformUtils.java:44)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.updateServerPlatform (EntityManagerSetupImpl.java:973)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.predeploy(EntityMana gerSetupImpl.java:1718)
... 6 more

最佳答案

您可能放错了 EclipseLink jar 文件。

NPE (NullPointerException) 源于 org.eclipse.persistence.platform.server.NoServerPlatformDetector 中的第 28 行(请参阅您发布的错误中的最后一个 Caused by)。此行显示在 GrepCode here 上并显示:
PrivilegedAccessHelper.getClassLoaderForClass(NoServerPlatformDetector.class).toString();
并查看PrivilegedAccessHelper.getClassLoaderForClass方法表明该行可以重写为:
NoServerPlatformDetector.class.getClassLoader().toString();

现在,getClassLoader可以回null在这种情况下 toString()会导致NPE。 getClassLoader返回 null如果 EclipseLink jar 文件是使用引导类加载器加载的(参见 this wiki-page 关于引导类加载器)。这让我相信您将 EclipseLink jar 文件放在了 <JAVA_HOME>/jre/lib 中目录,或者用 Eclipse 的术语来说,使它成为“JRE 系统库”的一部分。
右键单击 Eclipse 中的项目,选择属性,选择“Java 构建路径”,打开选项卡“库”,展开“JRE 系统库”(或类似的东西)并确保 EclipseLink jar 文件不是显示。使用“添加(外部)JAR”按钮添加 EclipseLink jar 文件(这应确保使用系统类加载器而不是引导类加载器加载 jar 文件)。

在侧节点上,NoServerPlatformDetector导致 NPE 存在缺陷:下一行显示 if (loaderStr != null但是这个检查是多余的,因为 toString()永远不会回来null .换句话说:程序员知道 getClassLoader可以返回null,但是忘记了toString()在这种情况下会导致 NPE。

关于java - PersistenceUnitLoadingException - 创建 EntityManagerFactory 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30143887/

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