0

我收到此错误:

... 32 more
Caused by: java.lang.IllegalStateException: Attempting to execute an operation on a closed EntityManagerFactory.

我想要做的是创建一个实体类 User 和一个会话 Bean 来访问 User..

执行时出现错误:

User result = null;
String q = "SELECT user FROM " + User.class.getName() + " user WHERE user.username LIKE :username";
Query query = entityManager.createQuery(q).setParameter("username", username);
List<User> users = query.getResultList();
if (users.size() > 0) {
    result = users.get(0);
    System.out.println(result);
}

persistence.xml 看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/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">
  <persistence-unit name="DS3-ejbPU" transaction-type="JTA">
    <jta-data-source>db_ds_hw3</jta-data-source>
    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    <properties/>
  </persistence-unit>
</persistence>

编辑1:

注入代码:

 @PersistenceContext(unitName = "DS3-ejbPU")
    private EntityManager em;
4

2 回答 2

0

您很可能在重新部署时遇到了 GlassFish 错误,该错误已在 4.0 版中修复(尚未发布)。

目前,如上述链接中所述,解决方法是取消部署 - 重新部署应用程序。


相关

java.lang.IllegalStateException:尝试对关闭的 EntityManagerFactory 执行操作

于 2012-11-30T10:59:32.420 回答
0

我也遇到过这个错误,重新加载帮助了我。在控制台输入:

asadmin restart-domain
于 2013-09-18T22:10:06.640 回答