I am in the process of modularizing a Java EE application (Java EE 7, Glassfish 4, JPA 2.1, EclipseLink 2.5). Everything worked fine so far, but I am currently experiencing a showstopper.

---

Scenario:

I have an EAR called "myApp.ear" that has dependencies to two EJB/Entity modules represented as JARs called "myModule1.jar" and "myModule2.jar". "myModule2.jar" is also dependent of "myModule1.jar". Each module defines its own persistence.xml in META-INF. The EAR also defines a persistence.xml which serves as the composite persistence.xml.

I have configured everything as explained in the EclipseLink doc.

Everything is working fine: Interaction with JSF modules, EntityManager operations like find(), persist(), etc. Except one thing is constantly going wrong trying to use a named query: "NamedQuery of name XYZ not found". The named queries are all defined in the entities via @NamedQuery. In the unmodularized recent version of the application they are all working fine, I have not changed the original and working code at any point.

I have altered almost every possible setting in the persistence.xml´s like <class>, <mapping-file> and so on. I have varied the persistence.xml´s to death, tried all and everything I could think of, searched the net a hundred times, but sadly there is no solution in sight. I don´t care how I am getting my modularized app to work, I just want it to work without "NamedQuery of name XYZ not found". Any help is greatly appreciated. Here are my persistence units:

java.lang.IllegalArgumentException: NamedQuery of name: User.selectByEmail not found.
at org.eclipse.persistence.internal.jpa.QueryImpl.getDatabaseQueryInternal(QueryImpl.java:350)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.createNamedQuery(EntityManagerImpl.java:1107)
at com.sun.enterprise.container.common.impl.EntityManagerWrapper.createNamedQuery(EntityManagerWrapper.java:522)
at de.snc.backend.core.control.GenericQueryService.findWithNamedQuery(GenericQueryService.java:35)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.glassfish.ejb.security.application.EJBSecurityManager.runMethod(EJBSecurityManager.java:1081)
at org.glassfish.ejb.security.application.EJBSecurityManager.invoke(EJBSecurityManager.java:1153)
at com.sun.ejb.containers.BaseContainer.invokeBeanMethod(BaseContainer.java:4695)
at com.sun.ejb.EjbInvocation.invokeBeanMethod(EjbInvocation.java:630)
at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:582)
at org.jboss.weld.ejb.AbstractEJBRequestScopeActivationInterceptor.aroundInvoke(AbstractEJBRequestScopeActivationInterceptor.java:46)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
at com.sun.ejb.EjbInvocation.proceed(EjbInvocation.java:582)
at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.doCall(SystemInterceptorProxy.java:163)
at com.sun.ejb.containers.interceptors.SystemInterceptorProxy.aroundInvoke(SystemInterceptorProxy.java:140)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.sun.ejb.containers.interceptors.AroundInvokeInterceptor.intercept(InterceptorManager.java:883)
at com.sun.ejb.containers.interceptors.AroundInvokeChainImpl.invokeNext(InterceptorManager.java:822)
at com.sun.ejb.containers.interceptors.InterceptorManager.intercept(InterceptorManager.java:369)
at com.sun.ejb.containers.BaseContainer.__intercept(BaseContainer.java:4667)
at com.sun.ejb.containers.BaseContainer.intercept(BaseContainer.java:4655)
at com.sun.ejb.containers.EJBLocalObjectInvocationHandler.invoke(EJBLocalObjectInvocationHandler.java:212)
... 97 more

I´ve spent another several hours to find a solution ... no luck so far.

Can someone please tell me if my approach is common and valid.
Did I make a mistake configuring composite persistence.xml and member persistence.xml?
Are there any other approaches out there modularizing a Java EE App (1 Module = 1 Jar)? Maybe without composite persistence.xml?

Ok, this was somehow a fight, but finally it works (although I am not using composite persistence units any more).

If anyone is searching for a solution to modularize a Java EE 7 application that will be deployed to Glassfish, these are very good references:

monica2nto.wordpress.com/2012/01/18/dal-modularizada-en-jpa-con-un-unico-persistenceunit-j2ee/
-> It´s in Spanish, but just translate in a language you understand (e.g. via Google Chrome). The explanations for Websphere also work for Glassfish 4.

stackoverflow.com/a/14971161
stackoverflow.com/a/4076433
-> The same solution as adove, just in other words

If you want any further explanations modularizing the web tier take a look at this, it helped me a lot:
ocpsoft.org/java/jsf-java/how-to-modular-jsf-applications-with-cdi-and-prettyfaces/
stackoverflow.com/a/8320738