我正在尝试使用JPA为我目前参与的Java-EE项目设置持久性,并且我遇到了很多配置问题。目前,我已在persistence.xml中定义了一个RESOURCE_LOCAL持久性单元,目的是在解决设置JTA持久性单元及其相应JTA数据源的问题之前,在单元测试中使用此单元并验证其他所有工作。然而,即使这种“简单”的情况也会导致错误。
详细说明:
我得到的错误是:
javax.persistence.PersistenceException: No Persistence provider for EntityManager named kronosTestLocal
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:69)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:47)
at persistence.EntityManagerTest.getStaff(EntityManagerTest.java:56)
at persistence.EntityManagerTest.setup(EntityManagerTest.java:17)
...
抛出错误的代码片段是JUnit测试中的以下内容:
private final String persistenceUnitName = "kronosTestLocal";
....
final EntityManagerFactory entityManagerFactory =
Persistence.createEntityManagerFactory(persistenceUnitName);
persistence.xml(在META-INF内部并在intellij JPA facet中声明): (我尝试删除kronos持久性单元以防它以某种方式干扰,但这没有帮助)
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="2.0">
<persistence-unit name="kronos" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/kronos-database</jta-data-source>
<class>persistence.entities.DaySlotsEntity</class>
<class>persistence.entities.DaysEntity</class>
<class>persistence.entities.EmailNotificationsEntity</class>
<class>persistence.entities.EmailSendingTimestampsEntity</class>
<class>persistence.entities.GroupSlotsEntity</class>
<class>persistence.entities.GroupsEntity</class>
<class>persistence.entities.InterviewerEntity</class>
<class>persistence.entities.InterviewerPreferencesEntity</class>
<class>persistence.entities.InterviewerUnavailableDaysEntity</class>
<class>persistence.entities.InterviewerWorkloadEntity</class>
<class>persistence.entities.InterviewsEntity</class>
<class>persistence.entities.PreferenceTypesEntity</class>
<class>persistence.entities.PreferencesEntity</class>
<class>persistence.entities.StaffEntity</class>
<class>persistence.entities.StudentsEntity</class>
<class>persistence.entities.SwapsEntity</class>
</persistence-unit>
<persistence-unit name="kronosTestLocal" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>persistence.entities.DaySlotsEntity</class>
<class>persistence.entities.DaysEntity</class>
<class>persistence.entities.EmailNotificationsEntity</class>
<class>persistence.entities.EmailSendingTimestampsEntity</class>
<class>persistence.entities.GroupSlotsEntity</class>
<class>persistence.entities.GroupsEntity</class>
<class>persistence.entities.InterviewerEntity</class>
<class>persistence.entities.InterviewerPreferencesEntity</class>
<class>persistence.entities.InterviewerUnavailableDaysEntity</class>
<class>persistence.entities.InterviewerWorkloadEntity</class>
<class>persistence.entities.InterviewsEntity</class>
<class>persistence.entities.PreferenceTypesEntity</class>
<class>persistence.entities.PreferencesEntity</class>
<class>persistence.entities.StaffEntity</class>
<class>persistence.entities.StudentsEntity</class>
<class>persistence.entities.SwapsEntity</class>
<properties>
<property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/kronos"/>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
<property name="hibernate.connection.username" value="postgres"/>
<property name="hibernate.connection.password" value="****"/>
<property name="show_sql" value="true"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
</properties>
</persistence-unit>
</persistence>
Maven pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>kronos</artifactId>
<groupId>kronos</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>ejb</artifactId>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>3.6.7.Final</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.2-1003-jdbc4</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.6</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-ejb-plugin</artifactId>
<version>2.3</version>
<configuration>
<ejbVersion>3.2</ejbVersion>
<archive>
<manifest>
<addClasspath>true</addClasspath>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
从我的研究中我看到很多人遇到类似的问题,但从我所知道的我没有做任何常见的错误。我们中的一些人一直试图解决这个问题一段时间,所以任何帮助都会受到欢迎。
答案 0 :(得分:9)
如果您是通过JUnit测试并使用maven执行此操作,则persistence.xml
应位于src/test/resources/META-INF/persistence.xml
中,该测试将在测试执行时放入正确的位置。 src/main/resources/META-INF/
中的文件未使用,因为它不在test-jar
的路径中。