我花了最后几天寻找错误,但我无法找到它。我不得不说我是JPA的全新手,这是我的第一个项目,我是根据教程创建的,但却有一个无法解释的错误。
我使用Eclipse和Eclipselink并连接了runniny mySql-database
我的代码:
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
答案 0 :(得分:0)
您可能放错了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。如果使用bootstrap类加载器加载EclipseLink jar文件,则getClassLoader
返回null
(有关引导类加载器,请参阅this wiki-page)。这让我相信您将EclipseLink jar文件放在<JAVA_HOME>/jre/lib
目录中,或者以Eclipse术语表示,使其成为&#34; JRE系统库&#34;的一部分。
在Eclipse中右键单击项目,选择Properties,选择&#34; Java Build Path&#34;,打开选项卡&#34; Libraries&#34;,展开&#34; JRE系统库&#34; (或类似的东西)并确保未显示EclipseLink jar文件。使用&#34;添加(外部)JAR&#34;添加EclipseLink jar文件。按钮(这应该确保使用系统类加载器而不是引导类加载器加载jar文件)。
在辅助节点上,导致NPE的NoServerPlatformDetector
存在缺陷:下一行显示if (loaderStr != null
,但此检查是多余的,因为toString()
永远不会返回null
。换句话说:程序员知道getClassLoader
可以返回null,但忘记了toString()
在这种情况下会导致NPE。
答案 1 :(得分:0)
这是正确的解决方案。如果将eclipselink.jar和/或javax.persistence.jar放在用户库中,则eclipse无法找到EntityManagerFactory。因此,您必须删除用户库并将jar文件放在JRE系统库中。
右键点击项目&gt;构建路径&gt;配置构建路径&gt; Java构建路径(库)&gt;添加外部Jar&gt;在JRE系统库下添加eclipselink.jar并瞧...