我得到這個錯誤:EJB - 在實體管理器的依賴注入錯誤
... 32 more
Caused by: java.lang.IllegalStateException: Attempting to execute an operation on a closed EntityManagerFactory.
我想要做的就是創建一個實體類用戶和會話Bean來訪問用戶..
我當我執行出現錯誤:
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;
添加註入EntityManager的代碼部分。 –
看起來不錯。您可以嘗試ping(通過Glassfish)您的JDBC連接池「db_ds_hw3」。 –