[英]Would providing `EntityManagerFactory` solve `Unable to retrieve EntityManagerFactory for unitName xyz`
This is the (bottom) error from the stack trace:这是堆栈跟踪中的(底部)错误:
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)
...
The code is as follows (inspired by a @BalusC
's example I found on SO today):代码如下(灵感来自我今天在 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();
}
}
As reference, this is my persistence.xml (webmodule-root:Configuration files:作为参考,这是我的persistence.xml(webmodule-root:配置文件:
WEB-INFMETA-INF:persistence.xml 网络信息
META-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>
It seems, that you placed your persistence.xml file in wrong location.看来,您将persistence.xml 文件放在了错误的位置。 It should by located in /META-INF/ folder and not in WEB-INF.
它应该位于 /META-INF/ 文件夹中,而不是位于 WEB-INF 中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.