我刚刚切换到 Intellij IDEA,我正在尝试使用 RESOURCE_LOCAL 持久性单元测试我的 JPA 实体。我的环境目前正在使用 EclipseLink、MySQL 和 Maven。
目前我的测试类正在执行以下调用:
EntityManagerFactory factory = Persistence.createEntityManagerFactory("app-test");
正在检测持久性单元(如果我在 persistence.xml 文件中更改名称,我会收到一条错误消息,指出无法找到该单元)。但是,如果我运行测试,我会在控制台中收到以下错误:
Exception in thread "main" Local Exception Stack:
Exception [EclipseLink-30005] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.PersistenceUnitLoadingException
Exception Description: An exception was thrown while searching for persistence archives with ClassLoader: sun.misc.Launcher$AppClassLoader@1005354a
Internal Exception: javax.persistence.PersistenceException: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [app-test] failed.
Internal Exception: java.lang.NullPointerException
at org.eclipse.persistence.exceptions.PersistenceUnitLoadingException.exceptionSearchingForPersistenceResources(PersistenceUnitLoadingException.java:127)
at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:115)
at javax.persistence.Persistence.createEntityManagerFactory(Unknown Source)
at javax.persistence.Persistence.createEntityManagerFactory(Unknown Source)
at suite.Suite.main(Suite.java:13)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: javax.persistence.PersistenceException: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [app-test] failed.
Internal Exception: java.lang.NullPointerException
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.createPredeployFailedPersistenceException(EntityManagerSetupImpl.java:1541)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:1532)
at org.eclipse.persistence.internal.jpa.deployment.JPAInitializer.callPredeploy(JPAInitializer.java:98)
at org.eclipse.persistence.jpa.PersistenceProvider.createEntityManagerFactory(PersistenceProvider.java:105)
... 8 more
Caused by: Exception [EclipseLink-28018] (Eclipse Persistence Services - 2.4.0.v20120608-r11652): org.eclipse.persistence.exceptions.EntityManagerSetupException
Exception Description: Predeployment of PersistenceUnit [app-test] failed.
Internal Exception: java.lang.NullPointerException
at org.eclipse.persistence.exceptions.EntityManagerSetupException.predeployFailed(EntityManagerSetupException.java:221)
... 12 more
Caused by: java.lang.NullPointerException
at org.eclipse.persistence.internal.jpa.metadata.MetadataProject.hasEntity(MetadataProject.java:1272)
at org.eclipse.persistence.internal.jpa.metadata.accessors.objects.MetadataAnnotatedElement.isOneToMany(MetadataAnnotatedElement.java:610)
at org.eclipse.persistence.internal.jpa.metadata.accessors.classes.ClassAccessor.buildAccessor(ClassAccessor.java:594)
at org.eclipse.persistence.internal.jpa.metadata.accessors.classes.ClassAccessor.addAccessorFields(ClassAccessor.java:423)
at org.eclipse.persistence.internal.jpa.metadata.accessors.classes.ClassAccessor.addAccessors(ClassAccessor.java:395)
at org.eclipse.persistence.internal.jpa.metadata.accessors.classes.ClassAccessor.preProcess(ClassAccessor.java:1178)
at org.eclipse.persistence.internal.jpa.metadata.accessors.classes.MappedSuperclassAccessor.preProcess(MappedSuperclassAccessor.java:724)
at org.eclipse.persistence.internal.jpa.metadata.accessors.classes.EntityAccessor.preProcess(EntityAccessor.java:555)
at org.eclipse.persistence.internal.jpa.metadata.MetadataProject.processStage1(MetadataProject.java:1649)
at org.eclipse.persistence.internal.jpa.metadata.MetadataProcessor.processORMMetadata(MetadataProcessor.java:531)
at org.eclipse.persistence.internal.jpa.deployment.PersistenceUnitProcessor.processORMetadata(PersistenceUnitProcessor.java:550)
at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.predeploy(EntityManagerSetupImpl.java:1469)
... 10 more
这是我的persistence.xml
<?xml version="1.0" encoding="UTF-8" ?>
<persistence version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="app-test" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/app" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
<property name="eclipselink.ddl-generation.output-mode" value="database" />
<property name="eclipselink.logging.level" value="FINEST" />
<property name="eclipselink.logging.level.sql" value="FINEST" />
<property name="eclipselink.logging.parameters" value="true" />
</properties>
</persistence-unit>
</persistence>
我的 pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.app.entities</groupId>
<artifactId>app-entities</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
<name>app-entities</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>javax.persistence</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.24</version>
</dependency>
</dependencies>
</project>
我的文件夹结构(我所有的实体都在 src/main/java 文件夹中):