gpt4 book ai didi

hibernate - javax.persistence.PersistenceException : No Persistence provider for EntityManager named

转载 作者:行者123 更新时间:2023-12-04 17:21:42 27 4
gpt4 key购买 nike

我正在尝试使用 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 方面声明):
(我尝试删除 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>

从我的研究中,我看到很多人遇到了类似的问题,但据我所知,我没有犯任何常见的错误。我们中的一些人已经尝试解决这个问题一段时间了,所以欢迎任何帮助。

最佳答案

如果您是从 JUnit 测试中执行此操作并使用 maven,则 persistence.xml应位于 src/test/resources/META-INF/persistence.xml它将在测试执行时放入正确的位置。 src/main/resources/META-INF/ 中的文件未使用,因为它不在 test-jar 中的路径。

关于hibernate - javax.persistence.PersistenceException : No Persistence provider for EntityManager named,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21975553/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com