JPA实体管理器工厂注入失败(JPA 2,Eclipselink,J2EE 6) [英] JPA Entity Manager Factory injection fails (JPA 2, Eclipselink, J2EE 6)
问题描述
我试图让一个小样本web应用程序正常运行,但我遇到了注入Entity Manager Factory的问题。
我的persistence.xml是如下所示;
< persistence version =2.0xmlns =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 =maintransaction-type =JTA>
< provider> org.eclipse.persistence.jpa.PersistenceProvider< / provider>
< jta-data-source> jdbc / Maindb< / jta-data-source>
<属性>
< property name =eclipselink.ddl-generationvalue =drop-and-create-tables/>
< property name =eclipselink.ddl-generation.output-modevalue =database/>
< / properties>
< / persistence-unit>
Web应用程序有两个功能;返回一个客户和一个物品列表。
CustomerResource对象如下注入实体管理器工厂:
@PersistenceUnit(unitName =main)
private EntityManagerFactory emf;
并通过以下代码查询持久层:
EntityManager em = emf.createEntityManager();
Customer customer =(Customer)em.find(Customer.class,customerID);
这种方法没有问题(我知道),我得到了返回的期望数据。 / p>
ItemResource对象对同一持久性单元执行相同的操作。
@PersistenceUnit(unitName =main)
私人EntityManagerFactory emf;
但注入失败,emf始终为空。
EntityManager em = emf.createEntityManager(); < - emf在这里为空
我不确定我在这里做错了什么,我的猜测是我正在使用实体管理器工厂。
任何帮助将不胜感激!谢谢
更新
我拿出麻烦的代码放入war文件供大家看看,这帮助我隔离了这个问题。
这个问题似乎与我使用的url模式有关。
web.xml
<?xml version =1.0encoding =UTF-8 ?>< web-app xmlns:xsi =http://www.w3.org/2001/XMLSchema-instance
xmlns =http://java.sun.com/xml/ns/ javaeexmlns: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_IDversion =2.5>
< listener>
< listener-class> com.sun.xml.ws.transport.http.servlet.WSServletContextListener< / listener-class>
< / listener>
< servlet>
< servlet-name>项目< / servlet-name>
< servlet-class> com.sun.xml.ws.transport.http.servlet.WSServlet< / servlet-class>
1< / load-on-startup>
< / servlet>
< servlet-mapping>
< servlet-name>项目< / servlet-name>
< url-pattern> / Item / *< / url-pattern>
< / servlet-mapping>
< servlet>
< servlet-name>客户< / servlet-name>
< servlet-class> com.sun.xml.ws.transport.http.servlet.WSServlet< / servlet-class>
1< / load-on-startup>
< / servlet>
< servlet-mapping>
< servlet-name>客户< / servlet-name>
< url-pattern> / Customer< / url-pattern>
< / servlet-mapping>
< / web-app>
sun-jaxws.xml:
<?xml version =1.0encoding =UTF-8?>
< endpoints xmlns =http://java.sun.com/xml/ns/jax-ws/ri/runtimeversion =2.0>
< endpoint name =Itemimplementation =com.test.item.ItemResourceurl-pattern =/ Item / */>
< endpoint name =Customerimplementation =com.test.customer.CustomerResourceurl-pattern =/ Customer/>
< / endpoints>
该资源现在有两种网络方法;
-
获取物品详细信息
类型:获取
路径:/
Web参数:物品id <
-
获取物品清单
类型:获取
路径:/ list
Web参数:项目颜色
Get Item Details
Type: Get
Path: /
Web param: item idGet Item List
Type: Get
Path: /list
Web param: Item Colour
通过url模式中的通配符,实体管理器始终为空。如果我删除了通配符,那么我可以成功请求一个项目,把我无法请求一个项目列表,因为它没有映射。
客户资源请求总是成功因为它在映射中不包含任何通配符。
谢谢
我不确定这会回答这个问题(为什么在第二种情况下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;
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;
这篇关于JPA实体管理器工厂注入失败(JPA 2,Eclipselink,J2EE 6)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!