jpa 持久性单元加载异常-创建实体管理器工厂时出错

6ljaweal  于 2022-11-14  发布在  其他
关注(0)|答案(3)|浏览(148)

我花了最后几天的时间来寻找错误,但是我没有找到。我必须说,我对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
e4yzc0pl

e4yzc0pl1#

您可能放错了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。如果使用引导类加载器加载EclipseLink jar文件,getClassLoader将返回null(请参阅this wiki-page中有关引导类加载器的内容)这让我相信您将EclipseLink jar文件放在了<JAVA_HOME>/jre/lib目录中,或者用Eclipse的术语来说,使其成为“JRE系统库”的一部分。
右键单击Eclipse中的项目,选择“属性”,选择“Java构建路径”,打开“库”选项卡,展开“JRE系统库”(或类似内容),并确保未显示EclipseLink jar文件。使用“添加(外部)JAR”按钮添加EclipseLink jar文件(这应确保使用系统类加载器而不是引导类加载器加载jar文件)。
在端节点上,导致NPE的NoServerPlatformDetector有缺陷:下一行显示if (loaderStr != null,但此检查是多余的,因为toString()永远不会返回null。换句话说:程序员知道getClassLoader可以返回null,但忘记了toString()在这种情况下会导致NPE。

xmjla07d

xmjla07d2#

这是正确的解决方案。如果您将eclipselink.jar和/或javax.persistence.jar放在用户库中,则eclipse无法找到EntityManagerFactory。因此,您必须删除用户库并将jar文件放在JRE系统库中。
右键单击项目〉构建路径〉配置构建路径〉Java构建路径(库)〉添加外部Jar〉在JRE系统库下添加eclipselink.jar,瞧...

wixjitnu

wixjitnu3#

在我的例子中,这个问题是由于在类的声明中没有使用“annotation”@而产生的。在我的例子中,字段类型DATE,不要在Java类的属性上使用“@Temporal(javax.persistence.TemporalType.DATE)”。当我添加这个注解时,问题就解决了。

相关问题