简体   繁体   中英

Packaging persistence.xml outside of jar with OpenJPA

So I'm using JPA (OpenJPA 2.3.0 implementation) in perhaps a rather unconventional way. 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. 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.

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. They could then refer to my jar file in persistence.xml. Something like the following (the name of my software is 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. I added my persistence.xml file and mapping XML to a jar called wam-config.jar and stuck it in the ear library folder. When I make the web service request, I get the following error back:

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. 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. 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.

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. 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! 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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