omnifaces / omnipersistence

Utilities for JPA, JDBC and DataSources
Other
31 stars 12 forks source link

EAR: Could not resolve a persistence unit corresponding to the persistence-unit-ref-name #5

Closed mydeadlyvenoms closed 6 years ago

mydeadlyvenoms commented 6 years ago

Hi there

First of all, I really think OmniPersistence in combination with OmniFaces and OptimusFaces will become the killer Java EE trio. The BaseEntityService is just awesome and will save many hours of repetitive coding.

Anyway - I am actually not sure if this is a real bug or a feature request. We tried to drop our generic (abstract) entity service and replace it with the one provided by OmniPersistence.

Our current project is basically an EAR containing multiple WAR and EAR modules. I guess due to this architecture OmniPersistence is not able to locate the Persistence Unit. It seems like OmniFaces is automatically trying to find the Persistence Unit (persistence.xml) within all the modules of the EAR. Since there is a module for the model, the Persistence Unit is only part this particular EJB module.

Thank you very much for your help!

[2017-09-18T14:37:40.211+0000] [Payara 4.1] [WARNING] [] [javax.enterprise.system.core.security] [tid: _ThreadID=149 _ThreadName=admin-thread-pool::admin-listener(7)] [timeMillis: 1505745460211] [levelValue: 900] [[
  JACC: For the URL pattern /*:/resources/*:/admin/*:/auth/*:/auth/targets/create:/public/*:/auth/targets/edit, the following methods were uncovered: POST,GET]]
[2017-09-18T14:37:40.215+0000] [Payara 4.1] [SEVERE] [] [javax.enterprise.system.core] [tid: _ThreadID=149 _ThreadName=admin-thread-pool::admin-listener(7)] [timeMillis: 1505745460215] [levelValue: 1000] [[
  Exception while invoking class org.glassfish.persistence.jpa.JPADeployer prepare method]]
[2017-09-18T14:37:40.216+0000] [Payara 4.1] [SEVERE] [] [javax.enterprise.system.core] [tid: _ThreadID=149 _ThreadName=admin-thread-pool::admin-listener(7)] [timeMillis: 1505745460216] [levelValue: 1000] [[
  Exception while invoking class org.glassfish.javaee.full.deployment.EarDeployer prepare method]]
[2017-09-18T14:37:40.217+0000] [Payara 4.1] [SEVERE] [] [] [tid: _ThreadID=149 _ThreadName=admin-thread-pool::admin-listener(7)] [timeMillis: 1505745460217] [levelValue: 1000] [[
  org.glassfish.deployment.common.DeploymentException: Could not resolve a persistence unit corresponding to the persistence-unit-ref-name [org.omnifaces.persistence.Environment/entityManagerFactory] in scope of the module called [blub-server-full-1.0-SNAPSHOT#ch-xxxx-blub-server-blub-server-yyy-1.0-SNAPSHOT.jar]. Please verify your application.
    at org.glassfish.javaee.full.deployment.EarDeployer.prepare(EarDeployer.java:182)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.prepareModule(ApplicationLifecycle.java:926)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:435)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:220)
    at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:488)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:544)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:540)
    at java.security.AccessController.doPrivileged(Native Method)
    at javax.security.auth.Subject.doAs(Subject.java:360)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$2.execute(CommandRunnerImpl.java:539)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$3.run(CommandRunnerImpl.java:570)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$3.run(CommandRunnerImpl.java:562)
    at java.security.AccessController.doPrivileged(Native Method)
    at javax.security.auth.Subject.doAs(Subject.java:360)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:561)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1469)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1300(CommandRunnerImpl.java:111)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1851)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1727)
    at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:534)
    at com.sun.enterprise.v3.admin.AdminAdapter.onMissingResource(AdminAdapter.java:224)
    at org.glassfish.grizzly.http.server.StaticHttpHandlerBase.service(StaticHttpHandlerBase.java:189)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:480)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:180)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
        ...
BalusC commented 6 years ago

Thank you for your issue report! We haven't tested this on multi module EARs yet.

Before trying to reproduce, may I ask where OmniPersistence JAR is placed? In EAR/lib? (where it belongs). And, do I correctly understand that you have only one persistence.xml residing in EAR/META-INF?

mydeadlyvenoms commented 6 years ago

Hi @BalusC , thank you for your response.

The structure basically looks like the following.

To answer your question, no the persistence.xml and OmniPersistence are part of the model (an EJB module). All other modules are using this EJB (Model) as provided dependency.

Please let me know if I can further assist.

mydeadlyvenoms commented 6 years ago

I just tried to move OmniPersistence into the Maven parent (EAR), the very same exception occurred.

BalusC commented 6 years ago

We got bitten by the fact that CDI @ApplicationScoped is EAR-wide and that didn't work very well in this construct. I reworked some code and you only need to make small changes in your end to override the new BaseEntityService#getEntityManager() method as described in its javadoc: https://github.com/omnifaces/omnipersistence/commit/d5b2fbd24f4cd42c9504c54301a42bf5bc867f46#diff-b95bff2acfb93244986448a03cee7a57R278

Returns the entity manager being used. When you have only one persistence unit, then you don't need to override this. When you have multiple persistence units, then you need to extend the BaseEntityService like below wherein you supply the persistence unit specific entity manager and then let all your service classes extend from it instead.

public abstract class YourBaseEntityService<E extends BaseEntity<Long>> extends BaseEntityService<Long, E> {

    @PersistenceContext(unitName = "yourPersistenceUnitName")
    private EntityManager entityManager;

    @Override
    public EntityManager getEntityManager() {
        return entityManager;
    }

}

Supply one in each EJB module wherein you specify the desired persistence unit name.

The fix is available in today's latest 0.4-SNAPSHOT. Let me know if that works out for you.

mydeadlyvenoms commented 6 years ago

Hi @BalusC

Thank you very much for your detailed response, unfortunately it still doesn't work in my case. Since I have only one persistence unit, I don't have to override the mentioned method. Anyway I tried both - same exception occurred in both cases.

[2017-09-25T06:25:57.479+0000] [Payara 4.1] [SEVERE] [] [] [tid: _ThreadID=118 _ThreadName=admin-thread-pool::admin-listener(7)] [timeMillis: 1506320757479] [levelValue: 1000] [[
  org.glassfish.deployment.common.DeploymentException: Could not resolve a persistence unit corresponding to the persistence-context-ref-name [org.omnifaces.persistence.audit.AuditListener/entityManager] in the scope of the module called [blub-server-full-1.0-SNAPSHOT#ch-xxxx-blub-server-blub-server-etc-1.0-SNAPSHOT.war]. Please verify your application.
    at org.glassfish.javaee.full.deployment.EarDeployer.prepare(EarDeployer.java:182)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.prepareModule(ApplicationLifecycle.java:926)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:435)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:220)
    at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:488)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:544)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:540)
    at java.security.AccessController.doPrivileged(Native Method)
    at javax.security.auth.Subject.doAs(Subject.java:360)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$2.execute(CommandRunnerImpl.java:539)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$3.run(CommandRunnerImpl.java:570)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$3.run(CommandRunnerImpl.java:562)
    at java.security.AccessController.doPrivileged(Native Method)
    at javax.security.auth.Subject.doAs(Subject.java:360)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:561)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1469)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1300(CommandRunnerImpl.java:111)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1851)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1727)
    at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:534)
    at com.sun.enterprise.v3.admin.AdminAdapter.onMissingResource(AdminAdapter.java:224)
    at org.glassfish.grizzly.http.server.StaticHttpHandlerBase.service(StaticHttpHandlerBase.java:189)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:480)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:180)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.portunif.PUFilter.handleRead(PUFilter.java:231)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.portunif.PUFilter.handleRead(PUFilter.java:231)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:539)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:593)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:573)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.RuntimeException: Could not resolve a persistence unit corresponding to the persistence-context-ref-name [org.omnifaces.persistence.audit.AuditListener/entityManager] in the scope of the module called [blub-server-full-1.0-SNAPSHOT#ch-zhaw-blub-server-blub-server-etc-1.0-SNAPSHOT.war]. Please verify your application.
    at com.sun.enterprise.deployment.BundleDescriptor.findReferencedPUViaEMRef(BundleDescriptor.java:733)
    at com.sun.enterprise.deployment.BundleDescriptor.findReferencedPUsViaPCRefs(BundleDescriptor.java:721)
    at org.glassfish.web.deployment.descriptor.WebBundleDescriptorImpl.findReferencedPUs(WebBundleDescriptorImpl.java:1053)
    at org.glassfish.persistence.jpa.JPADeployer.createEMFs(JPADeployer.java:188)
    at org.glassfish.persistence.jpa.JPADeployer.prepare(JPADeployer.java:170)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.prepareModule(ApplicationLifecycle.java:926)
    at org.glassfish.javaee.full.deployment.EarDeployer.prepareBundle(EarDeployer.java:309)
    at org.glassfish.javaee.full.deployment.EarDeployer.access$200(EarDeployer.java:88)
    at org.glassfish.javaee.full.deployment.EarDeployer$1.doBundle(EarDeployer.java:155)
    at org.glassfish.javaee.full.deployment.EarDeployer$1.doBundle(EarDeployer.java:152)
    at org.glassfish.javaee.full.deployment.EarDeployer.doOnBundles(EarDeployer.java:232)
    at org.glassfish.javaee.full.deployment.EarDeployer.doOnAllTypedBundles(EarDeployer.java:241)
    at org.glassfish.javaee.full.deployment.EarDeployer.doOnAllBundles(EarDeployer.java:267)
    at org.glassfish.javaee.full.deployment.EarDeployer.prepare(EarDeployer.java:152)
    ... 54 more
]] 
BalusC commented 6 years ago

Initial problem is solved. This is indeed the same exception, but now in another class which I overlooked during fixing. I'll check again.

mydeadlyvenoms commented 6 years ago

Hi @BalusC Let me know if I can assist.

BalusC commented 6 years ago

Retry with today's 0.4-SNAPSHOT.

mydeadlyvenoms commented 6 years ago

I just tested using the latest SNAPSHOT, "same" exception. Tried with and without a custom EntityService.

[2017-09-26T18:24:52.430+0000] [Payara 4.1] [SEVERE] [] [] [tid: _ThreadID=75 _ThreadName=admin-thread-pool::admin-listener(3)] [timeMillis: 1506450292430] [levelValue: 1000] [[
  org.glassfish.deployment.common.DeploymentException: Could not resolve a persistence unit corresponding to the persistence-context-ref-name [org.omnifaces.persistence.service.BaseEntityService/entityManager] in the scope of the module called [xxxx-server-full-1.0-SNAPSHOT#ch-blub-xxxx-server-xxxx-server-yyy-1.0-SNAPSHOT.war]. Please verify your application.
    at org.glassfish.javaee.full.deployment.EarDeployer.prepare(EarDeployer.java:182)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.prepareModule(ApplicationLifecycle.java:926)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:435)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.deploy(ApplicationLifecycle.java:220)
    at org.glassfish.deployment.admin.DeployCommand.execute(DeployCommand.java:488)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:544)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$2$1.run(CommandRunnerImpl.java:540)
    at java.security.AccessController.doPrivileged(Native Method)
    at javax.security.auth.Subject.doAs(Subject.java:360)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$2.execute(CommandRunnerImpl.java:539)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$3.run(CommandRunnerImpl.java:570)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$3.run(CommandRunnerImpl.java:562)
    at java.security.AccessController.doPrivileged(Native Method)
    at javax.security.auth.Subject.doAs(Subject.java:360)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:561)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.doCommand(CommandRunnerImpl.java:1469)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl.access$1300(CommandRunnerImpl.java:111)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1851)
    at com.sun.enterprise.v3.admin.CommandRunnerImpl$ExecutionContext.execute(CommandRunnerImpl.java:1727)
    at com.sun.enterprise.v3.admin.AdminAdapter.doCommand(AdminAdapter.java:534)
    at com.sun.enterprise.v3.admin.AdminAdapter.onMissingResource(AdminAdapter.java:224)
    at org.glassfish.grizzly.http.server.StaticHttpHandlerBase.service(StaticHttpHandlerBase.java:189)
    at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:480)
    at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:180)
    at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206)
    at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180)
    at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.portunif.PUFilter.handleRead(PUFilter.java:231)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.portunif.PUFilter.handleRead(PUFilter.java:231)
    at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
    at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
    at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
    at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:539)
    at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
    at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:593)
    at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:573)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.RuntimeException: Could not resolve a persistence unit corresponding to the persistence-context-ref-name [org.omnifaces.persistence.service.BaseEntityService/entityManager] in the scope of the module called [xxxx-server-full-1.0-SNAPSHOT#ch-blub-xxxx-server-xxxx-server-yyy-1.0-SNAPSHOT.war]. Please verify your application.
    at com.sun.enterprise.deployment.BundleDescriptor.findReferencedPUViaEMRef(BundleDescriptor.java:733)
    at com.sun.enterprise.deployment.BundleDescriptor.findReferencedPUsViaPCRefs(BundleDescriptor.java:721)
    at org.glassfish.web.deployment.descriptor.WebBundleDescriptorImpl.findReferencedPUs(WebBundleDescriptorImpl.java:1053)
    at org.glassfish.persistence.jpa.JPADeployer.createEMFs(JPADeployer.java:188)
    at org.glassfish.persistence.jpa.JPADeployer.prepare(JPADeployer.java:170)
    at com.sun.enterprise.v3.server.ApplicationLifecycle.prepareModule(ApplicationLifecycle.java:926)
    at org.glassfish.javaee.full.deployment.EarDeployer.prepareBundle(EarDeployer.java:309)
    at org.glassfish.javaee.full.deployment.EarDeployer.access$200(EarDeployer.java:88)
    at org.glassfish.javaee.full.deployment.EarDeployer$1.doBundle(EarDeployer.java:155)
    at org.glassfish.javaee.full.deployment.EarDeployer$1.doBundle(EarDeployer.java:152)
    at org.glassfish.javaee.full.deployment.EarDeployer.doOnBundles(EarDeployer.java:232)
    at org.glassfish.javaee.full.deployment.EarDeployer.doOnAllTypedBundles(EarDeployer.java:241)
    at org.glassfish.javaee.full.deployment.EarDeployer.doOnAllBundles(EarDeployer.java:267)
    at org.glassfish.javaee.full.deployment.EarDeployer.prepare(EarDeployer.java:152)
    ... 54 more
]]
BalusC commented 6 years ago

That's unfortunate. Looks like I have to strip off @PersistenceContext EntityManager from BaseEntityService too. I'll look into obtaining it programmatically as default implementation of getEntityManager().

mydeadlyvenoms commented 6 years ago

Thank you for your help - again, let me know if I can assist. I am still trying to figure out whats causing the issue...

BalusC commented 6 years ago

This exception will be thrown when there are multiple persistence units in the same application which are not isolated from each other in EJB module's classpath. This is not an uncommon situation though. Normally, you'd have to use @PersistenceContext(unitName="...") there where the EntityManager is to be injected, but this is undesireable in a general purpose library like OmniPersistence.

What you could do is to explicitly register org.omnifaces.persistence.service.BaseEntityService/entityManager in EJB module's ejb-jar.xml to be bound with the specific <persistence-unit-name>.

Given an example that your persistence.xml has <persistence-unit name="YourPersistenceUnitName">, then you could explicitly configure the EJB module's BaseEntityService to use exactly that persistence unit in EJB module's /META-INF/ejb-jar.xml as below:

<?xml version="1.0" encoding="UTF-8"?>
<ejb-jar
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/ejb-jar_3_2.xsd"
    version="3.2"
>
    <enterprise-beans>
        <session>
            <ejb-name>BaseEntityService</ejb-name>
            <ejb-class>org.omnifaces.persistence.service.BaseEntityService</ejb-class>
            <session-type>Stateless</session-type>
            <persistence-context-ref>
                <persistence-context-ref-name>org.omnifaces.persistence.service.BaseEntityService/entityManager</persistence-context-ref-name>
                <persistence-unit-name>YourPersistenceUnitName</persistence-unit-name>
            </persistence-context-ref>
        </session>
    </enterprise-beans>
</ejb-jar>

I'm for now not seeing a nice way to resolve this programmatically. If that works for you, then perhaps I'll remove the BaseEntityService#getEntityManager() method again to hide it from the enduser (so you do not need to override it anymore).

mydeadlyvenoms commented 6 years ago

Hi @BalusC Good explanation, the strange point is that I have only one Persistence Unit. So the persistence.xml file is part of only one EJB module (the model), it is the only module which is allowed to directly talk to the database. Anyway I will now try to add the ejb-jar.xml file to all EJB modules.

BalusC commented 6 years ago

You have only one PU? That's then indeed unexpected. I understood from the beginning on that you've multiple (as indicated by exception and project structure). Which GF/Payara version exactly are you using? Where exactly is persistence.xml located in the resulting deployment? Can you post its minimal contents which would reproduce the problem? How exactly do you normally inject your EntityManager (when not using OmniPersistence)? It should in an isolated EJB module always work with just @PersistenceContext without any name/unit attributes. Which build system are you using to produce the EAR? Can you make sure that you don't have duplicated the EJB module in flavor of some client-like JAR file elsewhere in webapp's own classpath?

mydeadlyvenoms commented 6 years ago

Yes, very weird. It's a big project with many modules, I'll try to build a minimal example. Is there another way I can share a sample app with you than via GitHub? This would make it a little easier for me.

To answer your questions:

@Stateless
public class BlubFacade extends AbstractFacade<Blub> {

    @PersistenceContext(unitName = "MyPU")
    private EntityManager entityManager;

    @Override
    protected EntityManager getEntityManager() {
        return entityManager;
    }

Update:

mydeadlyvenoms commented 6 years ago

I just refactored my code, you were right - it is not needed to write @PersistenceContext(unitName = "MyPU") - @PersistenceContext also works without any problems.

mydeadlyvenoms commented 6 years ago

Here is a screenshot of the unpacked EAR file, this is the final location of the persistence.xml file.

bildschirmfoto 2017-09-27 um 09 49 36

BalusC commented 6 years ago

I reproduced your problem on Payara 4.1.2.173 (before I was testing on WF 10.1).

Caused by: java.lang.RuntimeException: Could not resolve a persistence unit corresponding to the persistence-context-ref-name [org.omnifaces.persistence.service.BaseEntityService/entityManager] in the scope of the module called [example-ear#example-war.war]. Please verify your application.

I now very much suspect a GF/Payara specific bug.

cc: @arjantijms

mydeadlyvenoms commented 6 years ago

Shall we wait for @arjantijms or shall I escalate the issue directly to the Payara guys? I might need your help to defend the issue. :-)

BalusC commented 6 years ago

Even better, Arjan works at Payara.

BalusC commented 6 years ago

Okay, this is strange. It started to work after I copied EJB's main/resources/META-INF/persistence.xml into WAR's main/resources/META-INF/persistence.xml. No further modifications were needed. There was even no need to override the BaseEntityService#getEntityManager() method. Also not when there are multiple EJBs (each <persistence-unit> is copied into WAR's persistence.xml). I'll therefore just remove the getEntityManager() method to remove the implication that this needs to be overridden.

Let me know if this approach works for you as well.

mydeadlyvenoms commented 6 years ago

Hi @BalusC Thank you very much for your answer, did it also work when having multiple war files? I'll test it as soon as I am at home. :-)

BalusC commented 6 years ago

Thank you very much for your answer, did it also work when having multiple war files?

Yep, works too for me. GF/PY didn't even require an ejb-jar.xml with <persistence-context-ref> at all. However, when I tested the same on WildFly, it failed. I had to explicitly register each local EJB in ejb-jar.xml like below:

<enterprise-beans>
    <session>
        <ejb-name>PersonService</ejb-name>
        <persistence-context-ref>
            <persistence-context-ref-name>org.omnifaces.persistence.service.BaseEntityService/entityManager</persistence-context-ref-name>
            <persistence-unit-name>example-ejb1-pu</persistence-unit-name>
            <injection-target> 
                <injection-target-class>org.omnifaces.persistence.service.BaseEntityService</injection-target-class> 
                <injection-target-name>entityManager</injection-target-name>
            </injection-target>
        </persistence-context-ref>
    </session>
</enterprise-beans>

As there's unfortunately no way to use a wildcard in <ejb-name>, I'll bring back BaseEntityService#getEntityService() anyway.

mydeadlyvenoms commented 6 years ago

Hi @BalusC First of all - sorry for the severe delay. I tried your suggestions - unfortunately nothing worked in my case. The only way which works in my case is copying the persistence.xml into all WAR and EJB Modules. By doing so I was able to run my application (EAR) and finally use OmniPersistence. The solution feels a bit hacky, but I really don't know how to solve it in a cleaner way.

BalusC commented 6 years ago

Closing off handled issue.

david-uhlig commented 6 years ago

Just for confirmation: Copying the persistence.xml to the WAR module worked for me with Glassfish 5.0, Omnipersistence 0.5, too.