1

我是 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 实例。谢谢!

4

2 回答 2

0

你能试一下吗

public class MyServlet extends HttpServlet {
//this is thread-safe
@PersistenceUnit(unitName="MiddlewareExpedElectrModel")
private EntityManagerFactory emf;

据我所知,EntityManager 接口中的方法不是线程安全的,可能不会在多个并发请求之间共享。因此,不要将 EntityManager 注入到 servlet 实例变量中。

这不是线程安全的,并避免它

@PersistenceContext(unitName="MiddlewareExpedElectrModel")
private EntityManager em;

您仍然可以在 servlet 类类型级别注入 EntityManager,并在请求处理期间需要时进行查找。

@PersistenceContext(unitName="MiddlewareExpedElectrModel", name="persistence/em")
public class MyServlet extends HttpServlet {
protected void doGet(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
try {
  InitialContext ic = new InitialContext();
  EntityManager em =
   (EntityManager) ic.lookup("java:comp/env/persistence/em");
  } catch (NamingException ex) {
    ...
  }
于 2013-02-11T19:02:25.597 回答
0

我在 glassfish 服务器上的应用程序中遇到了同样的问题。在我从 web.xml 中删除 servlet 声明并使用注释声明 servlet 之前,entitymanager 工厂始终为空。

@WebServlet("/GetRevisioniServlet")  // <-- this annotation solved all my problems
public class GetRevisioniServlet extends HttpServlet {

@PersistenceUnit(unitName = "clerpPU")
private EntityManagerFactory entityManagerFactory;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

        EntityManager em = null;
        em = entityManagerFactory.createEntityManager(); 
        .....
    }
 }
于 2018-07-06T10:23:28.433 回答