我是JPA的begginer,當我在servlet中使用@PersistenceUnit時遇到問題。 EntityManagerFactory沒有被注入,我不知道爲什麼。EntityManagerFactory未被注入 - JPA
public class MyServlet extends HttpServlet
{
@PersistenceUnit
private EntityManagerFactory emf = null;
public MyServlet()
{
super();
}
@Override
public void init(ServletConfig config) throws ServletException
{
super.init(config);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Writer w = response.getWriter();
w.append("HELLO HELLO");
w.close();
}
}
我已經配置persistence.xml文件(META-INF目錄中)這樣的:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">
<persistence-unit name="MiddlewareExpedElectrModel" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>java:/OracleDS</non-jta-data-source>
<class>com.ieci.mugeju.middleware.model.entities.FechaUltimaSolicitudProcesada</class>
<properties>
<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
</properties>
</persistence-unit>
爲什麼不EntityManagerFactory的注入?
我正在使用Jboss 4.2.3,並且我還使用oracle-ds.xml文件配置了一個數據源。
UPDATE:
如果我試圖以編程方式獲得的EntityManagerFactory,效果很好:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("MiddlewareExpedElectrModel");
但我想通過注射獲得EntityManagerFactory實例。 謝謝!
任何人都可以幫助我嗎? :( – Eduardo 2013-02-13 09:30:11
您是否嘗試過使用'@PersistenceContext(unitName =「MiddlewareExpedElectrModel」,name =「persistence/em」)''public class MyServlet extends HttpServlet {'? – 2013-02-14 12:22:03
不,我沒有嘗試過,但我認爲我讀過@PersistenceContext一定不能用在HttpServlet中 – Eduardo 2013-02-15 10:13:26