我想寫我的第一個JPA Web應用程序,我卡住了。我有以下代碼:Java持久性 - 創建查詢失敗
@Stateless
public class TestDAO {
@PersistenceContext(unitName="dbcon")
EntityManager em;
public TestDAO() {
}
public TestCon getData() {
Query qry = em.createQuery("select t from TestCon t");
...
最後一行失敗 - 「java.lang.NullPointerException」。該TestCon代碼:
@Entity
@Table(name = "testcon", schema = "C##test")
public class TestCon {
@Id
@Column(name = "a")
private int a;
@Column(name = "b", length = 20)
private String b;
public String getB() {
return b;
}
}
而且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="dbcon" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/jboss/datasources/OracleDS</jta-data-source>
<class>code.TestCon</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect"/>
</properties>
</persistence-unit>
</persistence>
任何幫助表示讚賞。
你是否在使用Spring進行依賴注入?你的代碼永遠不會設置一個EntityManager ... –
不,不使用Spring。實體管理者不是從工廠創建的。它通過@PersistenceContext(unitName =「dbcon」) – user1680859
作爲資源從容器注入,因爲您可以清楚地看到,它不是... – Antoniossss