gpt4 book ai didi

java - junit 的异常 JPA derby 配置?

转载 作者:行者123 更新时间:2023-12-01 06:07:07 32 4
gpt4 key购买 nike

我有一个配置文件persistence.xml,如下所述:

<?xml version="1.0" encoding="UTF-8" ?>
<persistence 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"
version="2.0" xmlns="http://java.sun.com/xml/ns/persistence">
<persistence-unit name="entity"
transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<validation-mode>NONE</validation-mode>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver" />
<property name="javax.persistence.jdbc.url"
value="jdbc:derby:src/test/resources/sql/entityDB;create=true" >
<!-- EclipseLink should create the database schema automatically -->
<property name="eclipselink.ddl-generation" value="create-tables" />
<property name="eclipselink.ddl-generation.output-mode"
value="database" />
<property name="eclipselink.logging.level" value="ALL" />
</properties>
</persistence-unit>

并将持久性代码加载为:

public static void main(String[] args) {      
String persistenceUnit = "entity";
Properties pros = new Properties();
pros.setProperty(PersistenceUnitProperties.ECLIPSELINK_PERSISTENCE_XML,
"src/test/resources/META-INF/persistence.xml");
// Get the entity manager for the tests.
entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnit,pros);
entityManager = entityManagerFactory.createEntityManager();
Query q = entityManager.createQuery("select * from entityTables");
List<Entity> todoList = q.getResultList();

entityManager.close();
}

但是当我调用 createEntityManagerFactory 时,我收到以下异常:

Exception in thread "main" javax.persistence.PersistenceException:No Persistence provider for EntityManager named entity

如何修复它?

最佳答案

<persistence-unit name="entity" transaction-type="RESOURCE_LOCAL">之后,使用持久化提供者名称:

<provider>org.hibernate.ejb.HibernatePersistence</provider>

编辑:或将其放入 list 的类路径中: - persistence.jar - eclipselink.jar

关于java - junit 的异常 JPA derby 配置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42063156/

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