【发布时间】:2015-05-09 19:02:27
【问题描述】:
我最近几天都在寻找错误,但我找不到它。我不得不说我是 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
【问题讨论】:
-
你把persistence.xml放在哪里了?请写出文件路径。
-
persistence.xml 文件位于 META-INF 文件夹和 JPA-Content 下
-
您的persistence.xml 位于此处很重要:'projectroot'/src/main/resources/META-INF/persistence.xml。在我看来,它似乎没有找到“tutorialspoint_JPA_Eclipselink”持久性文件......
-
路径是:tutorialspoint_JPA_Eclipselink/src/META-INF 如何移动到你建议的路径?
-
好的。请尝试将您的路径更改为:src/main/resources/META-INF/persistence.xml 并让我知道它是否有效