方案如下。我在Eclipse上做一个J2EE项目。该项目是用Maven创建的。我正在尝试启动并运行我的单元测试,当我尝试创建EntityManager时,它会引发异常:
javax.persistence.PersistenceException:没有持久性提供程序 EntityManager名为primary。
@BeforeClass
public static void setUpClass() throws Exception {
Logger.getLogger("org").setLevel(Level.ALL);
EntityManagerFactory factory = Persistence.createEntityManagerFactory("primary.");
em = (EntityManager) factory.createEntityManager();
}
所以我通过从Persistent类中获取URLClassLoader来检查运行时类路径,其中包括:
/卷/ SVEN的/ dev / Eclipse的工作区/电子虚拟/ EJB /目标/测试类/ /卷/ SVEN的/ dev / Eclipse的工作区/电子虚拟/ EJB /目标/类/ /Volumes/SVEN/dev/jar_repo/mysql-connector-java-5.1.42/mysql-connector-java-5.1.42-bin.jar
META-INF目录位于它所属的位置:
E-虚拟/ EJB /目标/测试类/ META-INF / persistence.xml中 和 电子虚拟/ EJB /目标/类/ META-INF / persistence.xml中
我似乎只能通过以下方式访问persistence.xml: 的System.out.println(的getClass()的getResource( “../../../../../ META-INF / persistence.xml中”));
这真让我大吃一惊。我在这里缺少什么?
我已经清理过我的两次了。我真的很想念。
这是类路径的完整转储:
以下是完整列表:
/Volumes/SVEN/dev/eclipse-workspace/e-virtual/ejb/target/test-classes/
/Volumes/SVEN/dev/eclipse-workspace/e-virtual/ejb/target/classes/
/Volumes/SVEN/dev/jar_repo/mysql-connector-java-5.1.42/mysql-connector-java-5.1.42-bin.jar
/Users/sven/.m2/repository/org/jboss/spec/javax/ejb/jboss-ejb-api_3.1_spec/1.0.2.Final/jboss-ejb-api_3.1_spec-1.0.2.Final.jar
/Users/sven/.m2/repository/javax/enterprise/cdi-api/1.0-SP4/cdi-api-1.0-SP4.jar
/Users/sven/.m2/repository/org/jboss/spec/javax/interceptor/jboss-interceptors-api_1.1_spec/1.0.1.Final/jboss-interceptors-api_1.1_spec-1.0.1.Final.jar
/Users/sven/.m2/repository/javax/inject/javax.inject/1/javax.inject-1.jar
/Users/sven/.m2/repository/org/hibernate/javax/persistence/hibernate-jpa-2.0-api/1.0.1.Final/hibernate-jpa-2.0-api-1.0.1.Final.jar
/Users/sven/.m2/repository/org/hibernate/hibernate-validator/4.2.0.Final/hibernate-validator-4.2.0.Final.jar
/Users/sven/.m2/repository/javax/validation/validation-api/1.0.0.GA/validation-api-1.0.0.GA.jar
/Users/sven/.m2/repository/org/slf4j/slf4j-api/1.6.1/slf4j-api-1.6.1.jar
/Users/sven/.m2/repository/junit/junit/4.11/junit-4.11.jar
/Users/sven/.m2/repository/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar
/Users/sven/.m2/repository/org/jboss/arquillian/junit/arquillian-junit-container/1.1.0.Final/arquillian-junit-container-1.1.0.Final.jar
/Users/sven/.m2/repository/org/jboss/arquillian/junit/arquillian-junit-core/1.1.0.Final/arquillian-junit-core-1.1.0.Final.jar
/Users/sven/.m2/repository/org/jboss/arquillian/test/arquillian-test-api/1.1.0.Final/arquillian-test-api-1.1.0.Final.jar
/Users/sven/.m2/repository/org/jboss/arquillian/core/arquillian-core-api/1.1.0.Final/arquillian-core-api-1.1.0.Final.jar
/Users/sven/.m2/repository/org/jboss/arquillian/test/arquillian-test-spi/1.1.0.Final/arquillian-test-spi-1.1.0.Final.jar
/Users/sven/.m2/repository/org/jboss/arquillian/core/arquillian-core-spi/1.1.0.Final/arquillian-core-spi-1.1.0.Final.jar
/Users/sven/.m2/repository/org/jboss/arquillian/container/arquillian-container-test-api/1.1.0.Final/arquillian-container-test-api-1.1.0.Final.jar
/Users/sven/.m2/repository/org/jboss/shrinkwrap/shrinkwrap-api/1.1.2/shrinkwrap-api-1.1.2.jar
/Users/sven/.m2/repository/org/jboss/arquillian/container/arquillian-container-test-spi/1.1.0.Final/arquillian-container-test-spi-1.1.0.Final.jar
/Users/sven/.m2/repository/org/jboss/arquillian/core/arquillian-core-impl-base/1.1.0.Final/arquillian-core-impl-base-1.1.0.Final.jar
/Users/sven/.m2/repository/org/jboss/arquillian/test/arquillian-test-impl-base/1.1.0.Final/arquillian-test-impl-base-1.1.0.Final.jar
/Users/sven/.m2/repository/org/jboss/arquillian/container/arquillian-container-impl-base/1.1.0.Final/arquillian-container-impl-base-1.1.0.Final.jar
/Users/sven/.m2/repository/org/jboss/arquillian/config/arquillian-config-api/1.1.0.Final/arquillian-config-api-1.1.0.Final.jar
/Users/sven/.m2/repository/org/jboss/arquillian/config/arquillian-config-impl-base/1.1.0.Final/arquillian-config-impl-base-1.1.0.Final.jar
/Users/sven/.m2/repository/org/jboss/arquillian/container/arquillian-container-test-impl-base/1.1.0.Final/arquillian-container-test-impl-base-1.1.0.Final.jar
/Users/sven/.m2/repository/org/jboss/shrinkwrap/shrinkwrap-impl-base/1.1.2/shrinkwrap-impl-base-1.1.2.jar
/Users/sven/.m2/repository/org/jboss/shrinkwrap/shrinkwrap-spi/1.1.2/shrinkwrap-spi-1.1.2.jar
/Users/sven/.m2/repository/org/jboss/arquillian/protocol/arquillian-protocol-servlet/1.1.0.Final/arquillian-protocol-servlet-1.1.0.Final.jar
/Users/sven/.m2/repository/org/jboss/arquillian/container/arquillian-container-spi/1.1.0.Final/arquillian-container-spi-1.1.0.Final.jar
/Users/sven/.m2/repository/org/jboss/shrinkwrap/descriptors/shrinkwrap-descriptors-api-base/2.0.0-alpha-3/shrinkwrap-descriptors-api-base-2.0.0-alpha-3.jar
/Users/sven/.m2/repository/org/jboss/shrinkwrap/descriptors/shrinkwrap-descriptors-spi/2.0.0-alpha-3/shrinkwrap-descriptors-spi-2.0.0-alpha-3.jar
/Volumes/SVEN/dev/eclipse/jee-oxygen2/Eclipse.app/Contents/Eclipse/configuration/org.eclipse.osgi/1714/0/.cp/
/Volumes/SVEN/dev/eclipse/jee-oxygen2/Eclipse.app/Contents/Eclipse/configuration/org.eclipse.osgi/1713/0/.cp/
答案 0 :(得分:2)
问题不在于你的persistence.xml文件或它的位置,而是你的类路径上没有提供者JAR,所以它没有被插入。你应该通过maven为你想要的持久性提供者添加依赖性:例如Hibernate。
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
您可以使用OpenJPA(如果添加Java EE提供的依赖项
,则可以使用参考实现)<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
在类路径上有提供程序实现JAR后,JPA会自动获取它。