2
這是從堆棧跟蹤的(底部)錯誤:會提供`EntityManagerFactory`解決`無法檢索的EntityManagerFactory爲的unitName xyz`
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)
...
的代碼如下(由@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根:配置文件: WEB-INF 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>
我剛剛讀到'context'實際上是包含在'persistence.xml'中的信息,Persistencemanager從中被創建(所以我猜這裏沒有必要在文件本身中聲明並啓動它。仍然需要澄清我的'persistence.xml'有什麼問題呢? – developer10
@BalusC只是覺得你可能有興趣看看這個,因爲這實際上是基於你的一個例子(不知道它是否也使用了Hibernated,但無論如何...) – developer10
您是否嘗試過使用'@PersistenceContext(unitName =「MojaPU」)'? –