且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JPA实体管理器工厂注入失败(JPA 2,Eclipselink,J2EE 6)

更新时间:2023-09-19 14:40:52

我不确定这会回答这个问题(为什么在第二种情况下EMF null ),但是由于您使用的是应用程序管理的实体管理器,你是否正确地关闭了 EntityManager ?就像这样:

  public class LoginServlet extends HttpServlet {
@PersistenceUnit(unitName =EmployeeService)
EntityManagerFactory emf;

protected void doPost(HttpServletRequest请求,HttpServletResponse响应){
String userId = request.getParameter(user);
//检查有效用户
EntityManager em = emf.createEntityManager();
尝试{
User user = em.find(User.class,userId);
if(user == null){
// return error page
// ...
}
} finally {
em.close( );






$ b但是,说实话,我真的很怀疑你为什么不是'使用容器管理的实体管理器。在我看来,让容器管理其生命周期要简单得多。要获得 EntityManager 注入:
  @PersistenceContext(unitName =main )
private EntityManager em;


I am trying to get a small sample web app up and running but I have run into a problem injecting the Entity Manager Factory.

My persistence.xml is as follows;

<persistence version="2.0" xmlns=" http://java.sun.com/xml/ns/persistene"
         xmlns:xsi=" http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/persistence ttp://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="main" transaction-type="JTA">
    <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
    <jta-data-source>jdbc/Maindb</jta-data-source>
    <properties>
        <property name="eclipselink.ddl-generation" value="drop-and-create-tables" /> 
        <property name="eclipselink.ddl-generation.output-mode" value="database" /> 
    </properties>
</persistence-unit>

The web application has two functions; return a customer and a list of items.

The CustomerResource object injects the entity manager factory as follows:

@PersistenceUnit(unitName="main")
private EntityManagerFactory emf;

and queries the persistence layer by the following code;

EntityManager em = emf.createEntityManager();
Customer customer = (Customer) em.find(Customer.class, customerID);

This works with no problems (that I am aware of), I get the expected data returned.

The ItemResource object does the same thing against the same persistence unit.

@PersistenceUnit(unitName="main")
private EntityManagerFactory emf;

But the injection fails and emf is always null.

EntityManager em = emf.createEntityManager(); <- emf is null here

I am unsure of what I have done wrong here, my guess is that I am using the entity manager factory incorrectly.

Any help would be much appreciated! Thanks

Update

I was taking out the troublesome code to put in a war file for everyone to look at which helped me isolate the problem.

The issue seems to be with the url patterns I am using.

web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<listener>
    <listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>
<servlet>
    <servlet-name>Item</servlet-name>
    <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Item</servlet-name>
    <url-pattern>/Item/*</url-pattern>
</servlet-mapping>
<servlet>
    <servlet-name>Customer</servlet-name>
    <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Customer</servlet-name>
    <url-pattern>/Customer</url-pattern>
</servlet-mapping>
</web-app>

sun-jaxws.xml:

<?xml version="1.0" encoding="UTF-8"?>
<endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime" version="2.0">
    <endpoint name="Item" implementation="com.test.item.ItemResource" url-pattern="/Item/*" />
    <endpoint name="Customer" implementation="com.test.customer.CustomerResource" url-pattern="/Customer" />
</endpoints>

The item resource right now has two web methods;

  1. Get Item Details
    Type: Get
    Path: /
    Web param: item id

  2. Get Item List
    Type: Get
    Path: /list
    Web param: Item Colour

With the wild cards in the url patterns the entity manager is always null. If I remove the wild cards then I can successfully request an item, put I cannot request a list of items because it is not mapped.

The customer resource requests are always successful because it does not contain any wild cards in the mappings.

Thanks

I am not sure this will answer the question (why is the EMF null in the second case?) but since you're using an application-managed entity manager, do you close the EntityManager properly? Something like this:

public class LoginServlet extends HttpServlet {
    @PersistenceUnit(unitName="EmployeeService")
    EntityManagerFactory emf;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) {
        String userId = request.getParameter("user");
        // check valid user
        EntityManager em = emf.createEntityManager();
        try {
            User user = em.find(User.class, userId);
            if (user == null) {
                // return error page
                // ...
            }
        } finally {
            em.close();
    }
}

But, honestly, I really wonder why you aren't using a container-managed entity manager. It is much simpler to let the container manage its life cycle in my opinion. To get an EntityManager injected:

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