EAR with WAR accessing a JAR with JPA entities and persistence.xml
zecas Feb 23, 2011 9:57 AMHi,
I'm struggling with setting my persistence jar running in my web application, and maybe someone can give me a tip on doing this.
I've created a standalone java api package, it is a simple jar file, persist-1.0.0.jar, with the following structure:
[persist-1.0.0.jar]
|
+- META-INF
| |
| +- persistence.xml
|
+- com
|
+- jpa
|
+- Entity1.class
+- Entity2.class
...
My persistence.xml contents are:
<?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"
>
<persistence-unit name="myunit">
<jta-data-source>java:ds/MYUNIT</jta-data-source>
<class>com.jpa.Entity1</class>
<class>com.jpa.Entity2</class>
</persistence-unit>
</persistence>
Now I have a WAR project inside an EAR, let's call them "webapp-1.0.0.war" and "myear-1.0.0.ear" respectivelly.
At the moment, the jar is packaged at EAR level, and my WAR's MANIFEST.MF has a classpath reference to it (Class-Path: persist-1.0.0.jar).
My intentions are that I just use "persist-1.0.0.jar" in the war project without any reference. The "persist-1.0.0.jar" should be in the classpath, with all entity classes and "persistence.xml" definition file, still I cannot use them without creating a specific "persistence.xml" file INSIDE my WAR project, which references the entity classes or just references the jar file for it to search them.
Example:
public class MyListener implements ServletContextListener {
@PersistenceUnit(name="myunit")
private EntityManagerFactory emf;
...
}
Will give me a:
2011-02-23 11:09:35,127 ERROR [org.apache.catalina.core.StandardContext] (main) Context [/webapp] startup failed due to previous errors
java.lang.IllegalArgumentException: Container org.jboss.web.tomcat.service.TomcatInjectionContainer@18cdde3 failed to resolve persistence unit null
at org.jboss.injection.PersistenceUnitHandler.addPUDependency(PersistenceUnitHandler.java:135)
at org.jboss.injection.PersistenceUnitHandler.loadXml(PersistenceUnitHandler.java:75)
at org.jboss.web.tomcat.service.TomcatInjectionContainer.processMetadata(TomcatInjectionContainer.java:550)
at org.jboss.web.tomcat.service.WebCtxLoader.start(WebCtxLoader.java:158)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4272)
at org.jboss.web.tomcat.service.deployers.TomcatDeployment.performDeployInternal(TomcatDeployment.java:310)
at org.jboss.web.tomcat.service.deployers.TomcatDeployment.performDeploy(TomcatDeployment.java:142)
at org.jboss.web.deployers.AbstractWarDeployment.start(AbstractWarDeployment.java:461)
at org.jboss.web.deployers.WebModule.startModule(WebModule.java:118)
at org.jboss.web.deployers.WebModule.start(WebModule.java:97)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatcher.java:157)
at org.jboss.mx.server.Invocation.dispatch(Invocation.java:96)
...
at org.jboss.Main.boot(Main.java:221)
at org.jboss.Main$1.run(Main.java:556)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.IllegalArgumentException: Can't find a persistence unit named 'null' in AbstractVFSDeploymentContext@3259467{vfszip:/Java/jboss-5.1.0.GA/server/default/deploy/myear-1.0.0.ear/webapp-1.0.0.war/}
at org.jboss.jpa.resolvers.BasePersistenceUnitDependencyResolver.resolvePersistenceUnitSupplier(BasePersistenceUnitDependencyResolver.java:107)
at org.jboss.web.tomcat.service.TomcatInjectionContainer.resolvePersistenceUnitSupplier(TomcatInjectionContainer.java:685)
at org.jboss.injection.PersistenceUnitHandler.addPUDependency(PersistenceUnitHandler.java:130)
... 72 more
If I include a "persistence.xml" in the WAR:
<?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"
>
<persistence-unit name="myunit">
<jta-data-source>java:ds/MYUNIT</jta-data-source>
<jar-file>persist-1.0.0.jar</jar-file>
</persistence-unit>
</persistence>
It will work fine, I can even delete the persistence.xml inside the "persist-1.0.0.jar".
I would like it to load "persistence.xml" in the dependent jar, so it would give me a clean way of using the api jar.
Isn't this supposed to work like this? What am I doing wrong? What's the best practice for doing this?
Thanks