如何使用名为 student_pu_test 的 EntityManager 解决 javax.persistence.PersistenceException?
标题:\'javax.persistence.PersistenceException:StudentRepository 和 StudentRepositoryTest 类中没有针对 EntityManager‘student_pu_test’的持久性提供程序\'正文:我遇到了
标题:\'javax.persistence.PersistenceException:StudentRepository 和 StudentRepositoryTest 类中没有 EntityManager‘student_pu_test’的持久性提供程序\'
正文:我的 Java 应用程序中出现了一个持续性问题,即 javax.persistence.PersistenceException 错误。错误消息指出“没有名为 student_pu_test 的 EntityManager 的持久性提供程序”,并指向调用 Persistence.createEntityManagerFactory() 的行。尽管验证了我的 persistence.xml 配置、依赖项和 EntityManagerFactory 设置,问题仍然存在。
我有两个类涉及此问题, StudentRepository
和 StudentRepositoryTest
。以下是相关代码片段:
StudentRepository 类:
package fr.charly.jPATutorial;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class StudentRepository {
private EntityManager entityManager;
private EntityManagerFactory emf;
public StudentRepository() {
this.emf = Persistence.createEntityManagerFactory("student_pu_test");
this.entityManager = this.emf.createEntityManager();
}
public StudentRepository(String pu) {
this.emf = Persistence.createEntityManagerFactory(pu);
this.entityManager = this.emf.createEntityManager();
}
public Student add(Student student) {
entityManager.getTransaction().begin();
entityManager.persist(student);
entityManager.getTransaction().commit();
return student;
}
public Student find(Long id) {
return entityManager.find(Student.class, id);
}
public Student update(Student student) {
Student studentToUpdate = find(student.getId());
entityManager.getTransaction().begin();
studentToUpdate.setFirstName(student.getFirstName());
studentToUpdate.setLastName(student.getLastName());
entityManager.getTransaction().commit();
return studentToUpdate;
}
public void delete(Student student) {
entityManager.getTransaction().begin();
entityManager.remove(student);
entityManager.getTransaction().commit();
}
public void close() {
this.entityManager.close();
this.emf.close();
}
}
StudentRepositoryTest 类:
package fr.charly.jPATutorial;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
public class StudentRepositoryTest {
private static StudentRepository repository;
@BeforeAll
public static void beforeClass() throws Exception {
repository = new StudentRepository("student_pu_test");
}
@AfterAll
public static void afterClass() throws Exception {
if (repository != null) {
repository.close();
}
}
@BeforeEach
public void setUp() throws Exception {
}
@AfterEach
public void tearDown() throws Exception {
}
@Test
public void add() {
Student student = new Student();
student.setLastName("Red");
student.setFirstName("Alan");
repository.add(student);
assertNotNull(student.getId());
assertTrue(student.getId() != null);
}
@Test
public void find() {
Student student = new Student();
student.setLastName("Red");
student.setFirstName("Alan");
repository.add(student);
student = repository.find(student.getId());
assertNotNull(student);
assertNotNull(student.getId());
assertEquals("Red", student.getLastName());
}
@Test
public void update() {
Student student = new Student();
student.setLastName("Red");
student.setFirstName("Alan");
student = repository.add(student);
student.setLastName("Green");
student = repository.update(student);
assertNotNull(student);
assertEquals("Green", student.getLastName());
assertEquals("Alan", student.getFirstName());
}
@Test
public void delete() {
Student student = new Student();
student.setLastName("Red");
student.setFirstName("Alan");
student = repository.add(student);
repository.delete(student);
student = repository.find(student.getId());
assertNull(student);
}
}
持久性.xml
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
version="2.0">
<persistence-unit name="student_pu_test"
transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<class>fr.charly.jPATutorial.Student</class>
<properties>
<property name="javax.persistence.jdbc.url"
value="jdbc:h2:mem:test" />
<property name="javax.persistence.jdbc.driver"
value="org.h2.Driver" />
<property name="javax.persistence.jdbc.user" value="sa" />
<property name="javax.persistence.jdbc.password" value="" />
<property name="hibernate.dialect"
value="org.hibernate.dialect.H2Dialect" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.hbm2ddl.auto" value="create" />
</properties>
</persistence-unit>
</persistence>
错误信息
juin 05, 2024 9:18:01 AM javax.persistence.spi.PersistenceProviderResolverHolder$DefaultPersistenceProviderResolver log
AVERTISSEMENT: javax.persistence.spi::No valid providers found.
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.111 s <<< FAILURE! -- in fr.charly.jPATutorial.StudentRepositoryTest
[ERROR] fr.charly.jPATutorial.StudentRepositoryTest -- Time elapsed: 0.111 s <<< ERROR!
javax.persistence.PersistenceException: No Persistence provider for EntityManager named student_pu_test
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:85)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at fr.charly.jPATutorial.StudentRepository.<init>(StudentRepository.java:18)
at fr.charly.jPATutorial.StudentRepositoryTest.beforeClass(StudentRepositoryTest.java:20)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
[INFO]
[INFO] Results:
[INFO]
[ERROR] Errors:
[ERROR] StudentRepositoryTest.beforeClass:20 » Persistence No Persistence provider for EntityManager named student_pu_test
[INFO]
[ERROR] Tests run: 2, Failures: 0, Errors: 1, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.116 s
[INFO] Finished at: 2024-06-05T09:18:01+02:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:3.2.2:test (default-test) on project jPATutorial:
[ERROR]
[ERROR] Please refer to C:\Users\Charly\Desktop\developpement\workspace\jPATutorial\target\surefire-reports for the individual test results.
[ERROR] Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump, and [date].dumpstream.
我尝试检查我的配置、查找代码中的错误并检查在线资源,但还无法解决这个问题。
我将非常感激任何能帮助我排除故障并解决这一持续存在的问题的想法、建议或解决方案。提前感谢您的帮助和专业知识。