我花了最后几天寻找错误,但我找不到。我不得不说,我是一个完全新手JPA,这是我的第一个项目,我创建了一个教程,但有一个无法解释的错误。
我使用Eclipse和eclipseline连接了一个运行良好的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)。该行显示在GrepCodehere上,并显示:
PrivilegedAccessHelper.getClassLoaderForClass(NoServerPlatformDetector.class).toString();
通过查看PrivilegedAccessHelper.getClassLoaderForClass方法,可以将该行重写为:
NoServerPlatformDetector.class.getClassLoader().toString();
现在,getClassLoader可以返回null在这种情况下,toString()将导致NPE。getClassLoader如果EclipseLink jar文件是使用引导类加载器加载的,则返回null(请参见关于引导类加载器的this wiki-page)。这使我相信您将eclipseelinkjar文件放在<JAVA_HOME>/jre/lib目录中,或者用Eclipse术语来说,使它成为“JRE系统库”的一部分。
右键单击Eclipse中的项目,选择Properties,选择“Java Build Path”,打开“Libraries”选项卡,展开“JRE系统库”(或类似的东西),并确保没有显示eclipselinejar文件。使用“Add(external)JARs”按钮添加EclipseLink jar文件(这应该确保使用系统类加载器而不是引导类加载器加载jar文件)。
在侧节点上,导致NPE的NoServerPlatformDetector有缺陷:下一行显示if (loaderStr != null,但此检查是多余的,因为toString()永远不会返回null。换句话说:程序员知道getClassLoader可以返回null,但是忘记了toString()在这种情况下会导致NPE。

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

10-09 08:01