我是 JPA 的初学者,当我在 servlet 中使用 @PersistenceUnit 时遇到问题。EntityManagerFactory 没有被注入,我不知道为什么。
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 文件配置了一个数据源。
更新:
如果我尝试以编程方式获取 EntityManagerFactory,它运行良好:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("MiddlewareExpedElectrModel");
但我想通过注入获得一个 EntityManagerFactory 实例。谢谢!