gpt4 book ai didi

java - 提供 `EntityManagerFactory` 会解决 `Unable to retrieve EntityManagerFactory for unitName xyz`

转载 作者:行者123 更新时间:2023-11-29 02:47:49 26 4
gpt4 key购买 nike

这是来自堆栈跟踪的(底部)错误:

Caused by: java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory for unitName null
at com.sun.enterprise.container.common.impl.EntityManagerWrapper.init(EntityManagerWrapper.java:138)
at com.sun.enterprise.container.common.impl.EntityManagerWrapper._getDelegate(EntityManagerWrapper.java:171)
at com.sun.enterprise.container.common.impl.EntityManagerWrapper.createQuery(EntityManagerWrapper.java:477)
...

代码如下(灵感来自于今天在SO上找到的一个@BalusC的例子):

@Stateless
public class GradService {

@PersistenceContext
private EntityManager em;

public List<Grad> listAll() {
return em.createQuery("SELECT g FROM Grad g", Grad.class).getResultList();
}
}

作为引用,这是我的 persistence.xml (webmodule-root:Configuration files:WEB-INFMETA-INF:persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="MojaPU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/ekstraResource</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>

最佳答案

看来,您将 persistence.xml 文件放在了错误的位置。它应该位于/META-INF/文件夹中,而不是在 WEB-INF 中。

关于java - 提供 `EntityManagerFactory` 会解决 `Unable to retrieve EntityManagerFactory for unitName xyz`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39691221/

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