我收到此错误:
... 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;