Я провел последние дни в поисках ошибки, но я не смог ее найти. Я должен сказать, что я новичок в JPA, и это мой первый проект, который я создал после учебника, но имеющий необъяснимую ошибку.PersistenceUnitLoadingException - Ошибка при создании EntityManagerFactory
Я использую Eclipse, и EclipseLink и подсоединили runniny 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: [email protected]
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
Где вы кладете настойчивость. XML? Пожалуйста, напишите путь к файлу. – Mihai
Файл persistence.xml находится в META-INF-foulder и под JPA-контентом – OP13
Важно, чтобы ваш persistence.xml был здесь: 'projectroot'/src/main/resources/META-INF/persistence.xml. Мне кажется, что он не находит файл persistence tutorialspoint_JPA_Eclipselink ... – Mihai