简体   繁体   English

使用OpenJPA在jar外部打包persistence.xml

[英]Packaging persistence.xml outside of jar with OpenJPA

So I'm using JPA (OpenJPA 2.3.0 implementation) in perhaps a rather unconventional way. 因此,我可能以一种非常规的方式使用JPA(OpenJPA 2.3.0实现)。 I'm writing a piece of software that will maintain some personnel data and assign work to said personnel. 我正在写一个软件,该软件将维护一些人员数据并将工作分配给这些人员。 What type of work it assigns is undefined - the user must extend a mapped superclass, add any additional mappings for that class, and my software will basically take it from there. 它分配的工作类型是不确定的-用户必须扩展一个映射的超类,为该类添加任何其他映射,而我的软件基本上会从那里得到它。 We have several systems that follow this pattern, and we only wanted to write the assignment code once. 我们有几个遵循此模式的系统,我们只想编写一次分配代码。 However, there are small differences in what gets assigned - some system assign a case, other assign a smaller piece of a case, others assign based on a customer, etc. 但是,分配的内容之间存在细微差异-一些系统分配案例,其他系统分配较小的案例,其他系统根据客户分配,等等。

To get maximum configurability, I put all the metadata for my persistent entities into a mapping file instead of using annotations. 为了获得最大的可配置性,我将持久实体的所有元数据放入映射文件中,而不使用注释。 Since the programmer using my software would have to extend that mapped superclass and add their own mapping to the persistence unit, I didn't want to package persistence.xml or my mapping XML into the jar I'm creating. 由于使用我的软件的程序员必须扩展该映射的超类并将其自己的映射添加到持久性单元,因此我不想将persistence.xml或我的映射XML打包到正在创建的jar中。 I figured the programmer could include those files somewhere in their ear. 我认为程序员可以将这些文件放在耳朵的某个位置。 I wrote the entire thing using Java SE, not EE, so I'm creating the EntityManagerFactory myself instead of injecting it. 我使用Java SE而不是EE编写了整个内容,因此我自己创建了EntityManagerFactory而不是注入它。

My organization is using Websphere Application Server 7.0 which implements Java EE 5. Looking at the documentation for JPA in Java EE 5, it seemed my user would have to package the persistence.xml and mapping XML files into a separate jar which could be placed in the ear's library folder. 我的组织正在使用实现Java EE 5的Websphere Application Server 7.0。查看Java EE 5中JPA的文档,看来我的用户将不得不将persistence.xml打包并将XML文件映射到一个单独的jar中,该jar可以放在其中耳朵的资料夹。 They could then refer to my jar file in persistence.xml. 然后,他们可以在persistence.xml中引用我的jar文件。 Something like the following (the name of my software is WAM): 类似于以下内容(我的软件名称为WAM):

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
<persistence-unit name="WAM" transaction-type="RESOURCE_LOCAL">
    <mapping-file>wamMapping.xml</mapping-file>
    <jar-file>wam-1.0.0.jar</jar-file>
</persistence-unit>

I tried to write a quick and dirty web service in this manner. 我试图以这种方式编写快速而肮脏的Web服务。 I added my persistence.xml file and mapping XML to a jar called wam-config.jar and stuck it in the ear library folder. 我添加了persistence.xml文件,并将XML映射到名为wam-config.jar的jar中,并将其粘贴在ear库文件夹中。 When I make the web service request, I get the following error back: 提出Web服务请求时,出现以下错误:

javax.persistence.PersistenceException: Explicit persistence provider error(s) occurred for &quot;WAM&quot; after trying the following discovered implementations: com.ibm.websphere.persistence.PersistenceProviderImpl, org.apache.openjpa.persistence.PersistenceProviderImpl, org.apache.openjpa.persistence.PersistenceProviderImpl, org.apache.openjpa.persistence.PersistenceProviderImpl with the following failures:
com.ibm.websphere.persistence.PersistenceProviderImpl returned: java.lang.IllegalArgumentException: The jar resource &quot;wam-1.0.0.jar&quot; cannot be loaded.
at org.apache.openjpa.persistence.PersistenceUnitInfoImpl.validateJarFileName(PersistenceUnitInfoImpl.java:277)
at org.apache.openjpa.persistence.PersistenceUnitInfoImpl.processJarFileNames(PersistenceUnitInfoImpl.java:239)
at org.apache.openjpa.persistence.PersistenceProductDerivation.load(PersistenceProductDerivation.java:538)
at org.apache.openjpa.persistence.PersistenceProductDerivation.load(PersistenceProductDerivation.java:335)
at org.apache.openjpa.persistence.PersistenceProviderImpl.createEntityManagerFactory(PersistenceProviderImpl.java:89)
at com.ibm.websphere.persistence.PersistenceProviderImpl.createEntityManagerFactory(PersistenceProviderImpl.java:73)
at com.ibm.websphere.persistence.PersistenceProviderImpl.createEntityManagerFactory(PersistenceProviderImpl.java:43)
at org.apache.openjpa.persistence.PersistenceProviderImpl.createEntityManagerFactory(PersistenceProviderImpl.java:154)
at org.apache.openjpa.persistence.PersistenceProviderImpl.createEntityManagerFactory(PersistenceProviderImpl.java:65)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:150)
at gov.ssa.wam.service.BaseWAMService.initializeEntityManagerFactory(BaseWAMService.java:73)
at gov.ssa.wam.service.BaseWAMService.&lt;init&gt;(BaseWAMService.java:34)
at gov.ssa.wam.service.WAMServiceDelegate.getInstance(WAMServiceDelegate.java:31)
at gov.ssa.earnings.webservice.WAMServiceBean.&lt;init&gt;(WAMServiceBean.java:22)
at java.lang.J9VMInternals.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1474)
at com.ibm.wsspi.injectionengine.InjectionUtil.createInjectedInstance(InjectionUtil.java:79)
at com.ibm.ws.websvcs.server.WSServiceInstanceFactory.createServiceInstance(WSServiceInstanceFactory.java:110)
at org.apache.axis2.jaxws.server.EndpointController.handleRequest(EndpointController.java:247)
at org.apache.axis2.jaxws.server.EndpointController.invoke(EndpointController.java:103)
at org.apache.axis2.jaxws.server.JAXWSMessageReceiver.receive(JAXWSMessageReceiver.java:161)
at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:189)
at org.apache.axis2.transport.http.HTTPTransportUtils.processHTTPPostRequest(HTTPTransportUtils.java:275)
at com.ibm.ws.websvcs.transport.http.WASAxis2Servlet.doPost(WASAxis2Servlet.java:1431)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:738)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:831)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1663)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:939)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:502)
at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:181)
at com.ibm.ws.webcontainer.servlet.CacheServletWrapper.handleRequest(CacheServletWrapper.java:91)
at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:864)
at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1592)
at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:186)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:452)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:511)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:305)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInboundLink.java:276)
at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.sendToDiscriminators(NewConnectionInitialReadCallback.java:214)
at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.complete(NewConnectionInitialReadCallback.java:113)
at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165)
at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1646)
org.apache.openjpa.persistence.PersistenceProviderImpl returned: java.lang.IllegalArgumentException: The jar resource &quot;wam-1.0.0.jar&quot; cannot be loaded.
at org.apache.openjpa.persistence.PersistenceUnitInfoImpl.validateJarFileName(PersistenceUnitInfoImpl.java:277)
at org.apache.openjpa.persistence.PersistenceUnitInfoImpl.processJarFileNames(PersistenceUnitInfoImpl.java:239)
at org.apache.openjpa.persistence.PersistenceProductDerivation.load(PersistenceProductDerivation.java:538)
at org.apache.openjpa.persistence.PersistenceProductDerivation.load(PersistenceProductDerivation.java:335)
at org.apache.openjpa.persistence.PersistenceProviderImpl.createEntityManagerFactory(PersistenceProviderImpl.java:89)
at org.apache.openjpa.persistence.PersistenceProviderImpl.createEntityManagerFactory(PersistenceProviderImpl.java:154)
at org.apache.openjpa.persistence.PersistenceProviderImpl.createEntityManagerFactory(PersistenceProviderImpl.java:65)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:150)
at gov.ssa.wam.service.BaseWAMService.initializeEntityManagerFactory(BaseWAMService.java:73)
at gov.ssa.wam.service.BaseWAMService.&lt;init&gt;(BaseWAMService.java:34)
at gov.ssa.wam.service.WAMServiceDelegate.getInstance(WAMServiceDelegate.java:31)
at gov.ssa.earnings.webservice.WAMServiceBean.&lt;init&gt;(WAMServiceBean.java:22)
at java.lang.J9VMInternals.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1474)
at com.ibm.wsspi.injectionengine.InjectionUtil.createInjectedInstance(InjectionUtil.java:79)
at com.ibm.ws.websvcs.server.WSServiceInstanceFactory.createServiceInstance(WSServiceInstanceFactory.java:110)
at org.apache.axis2.jaxws.server.EndpointController.handleRequest(EndpointController.java:247)
at org.apache.axis2.jaxws.server.EndpointController.invoke(EndpointController.java:103)
at org.apache.axis2.jaxws.server.JAXWSMessageReceiver.receive(JAXWSMessageReceiver.java:161)
at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:189)
at org.apache.axis2.transport.http.HTTPTransportUtils.processHTTPPostRequest(HTTPTransportUtils.java:275)
at com.ibm.ws.websvcs.transport.http.WASAxis2Servlet.doPost(WASAxis2Servlet.java:1431)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:738)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:831)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1663)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:939)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:502)
at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:181)
at com.ibm.ws.webcontainer.servlet.CacheServletWrapper.handleRequest(CacheServletWrapper.java:91)
at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:864)
at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1592)
at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:186)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:452)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:511)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:305)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInboundLink.java:276)
at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.sendToDiscriminators(NewConnectionInitialReadCallback.java:214)
at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.complete(NewConnectionInitialReadCallback.java:113)
at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:165)
at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217)
at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161)
at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138)
at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204)
at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775)
at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1646)

The wam-1.0.0.jar file is in the ear library folder. wam-1.0.0.jar文件在ear库文件夹中。 It seems to be finding persistence.xml in wam-config.jar, but it can't find (or can't use) wam-1.0.0.jar. 似乎在wam-config.jar中找到了persistence.xml,但找不到(或无法使用)wam-1.0.0.jar。 Anyone have any idea where I'm going wrong? 有人知道我要去哪里错吗?

Edit: Posted complete stack trace per request. 编辑:每个请求发布完整的堆栈跟踪。 Sorry about that! 对于那个很抱歉!

I figured it out. 我想到了。 It seems that it couldn't find my mapping XML file. 似乎找不到我的映射XML文件。

I started experimenting by doing things the "classic" way - including the persistence.xml and my mapping XML file in the jar with my persisted entities. 我开始通过“经典”方式进行实验-包括persistence.xml和我的带有持久化实体的jar中的映射XML文件。 That wasn't working either. 那也不行。 Then, I changed the path to my mapping file in persistence.xml to "META-INF/wamMapping.xml" and things started working! 然后,我将persistence.xml中映射文件的路径更改为“ META-INF / wamMapping.xml”,事情开始起作用! After that, I removed persistence.xml and my mapping XML from my jar file and put them in a separate jar file inside a META-INF directory. 之后,我从jar文件中删除了persistence.xml和映射XML,并将它们放在META-INF目录内的另一个jar文件中。 I then placed that in my ear library. 然后,我将其放入我的耳朵库中。 Still working! 还在工作!

I'm now having trouble getting the additional persistent classes inside my war to work properly, but that is a different problem. 我现在很难使战争中的其他持久化类正常工作,但这是另一个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM