EJB - 实体管理器的依赖注入错误

时间:2012-11-30 10:49:44

标签: java-ee ejb ejb-3.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;

2 个答案:

答案 0 :(得分:0)

您最有可能遇到已在4.0版中修复的GlassFish bug in redeployment(尚未推出)。

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


相关

java.lang.IllegalStateException: Attempting to execute an operation on a closed EntityManagerFactory

答案 1 :(得分:0)

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

asadmin restart-domain