You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomee.apache.org by Hynek <hy...@gmail.com> on 2017/02/08 12:05:21 UTC

EntityManager/EntityManagerFactory not injected to CDI bean

Hello all!

I have an issue with injecting EntityManagerFactory (or EntityManager) into
a CDI managed bean.

My application is structured such that I have a root app.ear containing
persistence.jar and rest.war. When deployed to TomEE 7.0.2 it initialized
and starts up OK (no errors in the logs). persistence.jar defines my JPA
entities, persistence.xml with persistent unit, a service class referencing
EntityManager and providing business methods to the servlet classes in
rest.war:

EntityManager is being resolved with a CDI bean below. The motivation is,
besides to have a bit more flexibility with CDI, to be able to explicitly
drive the life time of the produced EntityManager instances:




And the service consuming the produced EntityManager:


Both classes above are packaged in peristence.jar.


Then in rest.war the service is consumed as follows:


As I have mentioned the ear deploys OK with no apparent errors. But when a
request comes to RestServer *the app fails with a NullPointerException
inside of EntityManagerProducer.newEntityManager() due to emFactory being
null, it is never set*.

Interesting to note, when I remove the @Inject annotation of Service.em and
use @PersistentContext directly there, the entity manager is properly
injected into the Service EJB and the REST request passes OK.

Any help is greatly appreciated!

Thanks,
Hynek




--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/EntityManager-EntityManagerFactory-not-injected-to-CDI-bean-tp4681031.html
Sent from the TomEE Users mailing list archive at Nabble.com.

Re: EntityManager/EntityManagerFactory not injected to CDI bean

Posted by Daniel Cunha <da...@gmail.com>.
Hynek,

Sorry the typo.

On Wed, Feb 8, 2017 at 10:57 AM, Daniel Cunha <da...@gmail.com> wrote:

> Hy Hynke,
>
> for use @Inject in your EntityManger you need to create a produce for that.
> A sampe for it, you can do something lke that:
>
> @ApplicationScoped
> public class JPAFactory {
>     private EntityManagerFactory emf = Persistence
>             .createEntityManagerFactory("MY_PU");
>
>     @Produces
>     @RequestScoped
>     public EntityManager getEntityManager() {
>         return emf.createEntityManager();
>     }
>
>     public void close(@Disposes EntityManager em) {
>         if (em.isOpen()) {
>             em.close();
>         }
>     }
> }
>
>
> Then will be possible to @Inject your EntityManager. :)
>
>
>
> On Wed, Feb 8, 2017 at 10:05 AM, Hynek <hy...@gmail.com> wrote:
>
>> Hello all!
>>
>> I have an issue with injecting EntityManagerFactory (or EntityManager)
>> into
>> a CDI managed bean.
>>
>> My application is structured such that I have a root app.ear containing
>> persistence.jar and rest.war. When deployed to TomEE 7.0.2 it initialized
>> and starts up OK (no errors in the logs). persistence.jar defines my JPA
>> entities, persistence.xml with persistent unit, a service class
>> referencing
>> EntityManager and providing business methods to the servlet classes in
>> rest.war:
>>
>> EntityManager is being resolved with a CDI bean below. The motivation is,
>> besides to have a bit more flexibility with CDI, to be able to explicitly
>> drive the life time of the produced EntityManager instances:
>>
>>
>>
>>
>> And the service consuming the produced EntityManager:
>>
>>
>> Both classes above are packaged in peristence.jar.
>>
>>
>> Then in rest.war the service is consumed as follows:
>>
>>
>> As I have mentioned the ear deploys OK with no apparent errors. But when a
>> request comes to RestServer *the app fails with a NullPointerException
>> inside of EntityManagerProducer.newEntityManager() due to emFactory being
>> null, it is never set*.
>>
>> Interesting to note, when I remove the @Inject annotation of Service.em
>> and
>> use @PersistentContext directly there, the entity manager is properly
>> injected into the Service EJB and the REST request passes OK.
>>
>> Any help is greatly appreciated!
>>
>> Thanks,
>> Hynek
>>
>>
>>
>>
>> --
>> View this message in context: http://tomee-openejb.979440.n4
>> .nabble.com/EntityManager-EntityManagerFactory-not-injected-
>> to-CDI-bean-tp4681031.html
>> Sent from the TomEE Users mailing list archive at Nabble.com.
>>
>
>
>
> --
> Daniel Cunha
> https://twitter.com/dvlc_
> http://www.tomitribe.com
> http://www.tomitribe.io
>



-- 
Daniel Cunha
https://twitter.com/dvlc_
http://www.tomitribe.com
http://www.tomitribe.io

Re: EntityManager/EntityManagerFactory not injected to CDI bean

Posted by Daniel Cunha <da...@gmail.com>.
Hy Hynke,

for use @Inject in your EntityManger you need to create a produce for that.
A sampe for it, you can do something lke that:

@ApplicationScoped
public class JPAFactory {
    private EntityManagerFactory emf = Persistence
            .createEntityManagerFactory("MY_PU");

    @Produces
    @RequestScoped
    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void close(@Disposes EntityManager em) {
        if (em.isOpen()) {
            em.close();
        }
    }
}


Then will be possible to @Inject your EntityManager. :)



On Wed, Feb 8, 2017 at 10:05 AM, Hynek <hy...@gmail.com> wrote:

> Hello all!
>
> I have an issue with injecting EntityManagerFactory (or EntityManager) into
> a CDI managed bean.
>
> My application is structured such that I have a root app.ear containing
> persistence.jar and rest.war. When deployed to TomEE 7.0.2 it initialized
> and starts up OK (no errors in the logs). persistence.jar defines my JPA
> entities, persistence.xml with persistent unit, a service class referencing
> EntityManager and providing business methods to the servlet classes in
> rest.war:
>
> EntityManager is being resolved with a CDI bean below. The motivation is,
> besides to have a bit more flexibility with CDI, to be able to explicitly
> drive the life time of the produced EntityManager instances:
>
>
>
>
> And the service consuming the produced EntityManager:
>
>
> Both classes above are packaged in peristence.jar.
>
>
> Then in rest.war the service is consumed as follows:
>
>
> As I have mentioned the ear deploys OK with no apparent errors. But when a
> request comes to RestServer *the app fails with a NullPointerException
> inside of EntityManagerProducer.newEntityManager() due to emFactory being
> null, it is never set*.
>
> Interesting to note, when I remove the @Inject annotation of Service.em and
> use @PersistentContext directly there, the entity manager is properly
> injected into the Service EJB and the REST request passes OK.
>
> Any help is greatly appreciated!
>
> Thanks,
> Hynek
>
>
>
>
> --
> View this message in context: http://tomee-openejb.979440.
> n4.nabble.com/EntityManager-EntityManagerFactory-not-injected-to-CDI-bean-
> tp4681031.html
> Sent from the TomEE Users mailing list archive at Nabble.com.
>



-- 
Daniel Cunha
https://twitter.com/dvlc_
http://www.tomitribe.com
http://www.tomitribe.io

Re: EntityManager/EntityManagerFactory not injected to CDI bean

Posted by Hynek <hy...@gmail.com>.
Romain Manni-Bucau wrote
> can you check the produces import before investing more time please?

import javax.enterprise.inject.Produces;




--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/EntityManager-EntityManagerFactory-not-injected-to-CDI-bean-tp4681031p4681041.html
Sent from the TomEE Users mailing list archive at Nabble.com.

Re: EntityManager/EntityManagerFactory not injected to CDI bean

Posted by Romain Manni-Bucau <rm...@gmail.com>.
can you check the produces import before investing more time please?


Romain Manni-Bucau
@rmannibucau <https://twitter.com/rmannibucau> |  Blog
<https://blog-rmannibucau.rhcloud.com> | Old Blog
<http://rmannibucau.wordpress.com> | Github <https://github.com/rmannibucau> |
LinkedIn <https://www.linkedin.com/in/rmannibucau> | JavaEE Factory
<https://javaeefactory-rmannibucau.rhcloud.com>

2017-02-08 14:13 GMT+01:00 Hynek <hy...@gmail.com>:

> Hi,
>
> I would say the class is scanned properly:
>
>
>
> Also I should mention that I do tomee.jpa.factory.lazy=true to prevent
> 'java.lang.IllegalStateException: On a thread without an initialized
> context
> nor a classloader mapping a deployed app'.
>
> Thanks,
> Hynek
>
>
>
> --
> View this message in context: http://tomee-openejb.979440.
> n4.nabble.com/EntityManager-EntityManagerFactory-not-injected-to-CDI-bean-
> tp4681031p4681038.html
> Sent from the TomEE Users mailing list archive at Nabble.com.
>

Re: EntityManager/EntityManagerFactory not injected to CDI bean

Posted by Hynek <hy...@gmail.com>.
Hi,

I would say the class is scanned properly:



Also I should mention that I do tomee.jpa.factory.lazy=true to prevent
'java.lang.IllegalStateException: On a thread without an initialized context
nor a classloader mapping a deployed app'.

Thanks,
Hynek



--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/EntityManager-EntityManagerFactory-not-injected-to-CDI-bean-tp4681031p4681038.html
Sent from the TomEE Users mailing list archive at Nabble.com.

Re: EntityManager/EntityManagerFactory not injected to CDI bean

Posted by Romain Manni-Bucau <rm...@gmail.com>.
oh, ear :).

Here is what tomee does for ears: it aligns CDI on the classloading
hierarchy (the least worse solution IMO),

ear/lib = one CDI context
webapp = one CDI context inherit most of the ear/lib Beans

if the producer is in the webapp it can't work if the consumer (@Inject) is
not. Seems you respected this logic if I got your first mail right so i
would recommand you to debug
in org.apache.openejb.cdi.WebappBeanManager#mergeBeans and check if you
producer is added
(org.apache.openejb.cdi.WebappBeanManager.InheritedBeanFilter#accept if the
filter logic)



Romain Manni-Bucau
@rmannibucau <https://twitter.com/rmannibucau> |  Blog
<https://blog-rmannibucau.rhcloud.com> | Old Blog
<http://rmannibucau.wordpress.com> | Github <https://github.com/rmannibucau> |
LinkedIn <https://www.linkedin.com/in/rmannibucau> | JavaEE Factory
<https://javaeefactory-rmannibucau.rhcloud.com>

2017-02-08 14:40 GMT+01:00 Hynek <hy...@gmail.com>:

> Romain Manni-Bucau wrote
> > if it helps here is a test with your code which works (tested with
> > 7.0.3-SNAPSHOT):
> > https://gist.github.com/rmannibucau/4acf9033372aceb2ae97a6986f40c4b7
>
> Thanks for the unit test. I can confirm it works OK in my environment, too.
> Is there anything else I can check to figure out why the same code doesn't
> produce the expected result in the real environment? Could it be the
> packaging structure that is causing trouble (jar, war in an ear)?
>
>
>
> --
> View this message in context: http://tomee-openejb.979440.
> n4.nabble.com/EntityManager-EntityManagerFactory-not-injected-to-CDI-bean-
> tp4681031p4681043.html
> Sent from the TomEE Users mailing list archive at Nabble.com.
>

Re: EntityManager/EntityManagerFactory not injected to CDI bean

Posted by Hynek <hy...@gmail.com>.
Romain Manni-Bucau wrote
> if it helps here is a test with your code which works (tested with
> 7.0.3-SNAPSHOT):
> https://gist.github.com/rmannibucau/4acf9033372aceb2ae97a6986f40c4b7

Thanks for the unit test. I can confirm it works OK in my environment, too.
Is there anything else I can check to figure out why the same code doesn't
produce the expected result in the real environment? Could it be the
packaging structure that is causing trouble (jar, war in an ear)?



--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/EntityManager-EntityManagerFactory-not-injected-to-CDI-bean-tp4681031p4681043.html
Sent from the TomEE Users mailing list archive at Nabble.com.

Re: EntityManager/EntityManagerFactory not injected to CDI bean

Posted by Romain Manni-Bucau <rm...@gmail.com>.
if it helps here is a test with your code which works (tested with
7.0.3-SNAPSHOT):
https://gist.github.com/rmannibucau/4acf9033372aceb2ae97a6986f40c4b7


Romain Manni-Bucau
@rmannibucau <https://twitter.com/rmannibucau> |  Blog
<https://blog-rmannibucau.rhcloud.com> | Old Blog
<http://rmannibucau.wordpress.com> | Github <https://github.com/rmannibucau> |
LinkedIn <https://www.linkedin.com/in/rmannibucau> | JavaEE Factory
<https://javaeefactory-rmannibucau.rhcloud.com>

2017-02-08 14:18 GMT+01:00 Hynek <hy...@gmail.com>:

> Romain Manni-Bucau wrote
> > Also is your unit a resource local one or jta one?
>
> The unit is jta.
>
>
>
>
> --
> View this message in context: http://tomee-openejb.979440.
> n4.nabble.com/EntityManager-EntityManagerFactory-not-injected-to-CDI-bean-
> tp4681031p4681040.html
> Sent from the TomEE Users mailing list archive at Nabble.com.
>

Re: EntityManager/EntityManagerFactory not injected to CDI bean

Posted by Hynek <hy...@gmail.com>.
Romain Manni-Bucau wrote
> Also is your unit a resource local one or jta one?

The unit is jta.




--
View this message in context: http://tomee-openejb.979440.n4.nabble.com/EntityManager-EntityManagerFactory-not-injected-to-CDI-bean-tp4681031p4681040.html
Sent from the TomEE Users mailing list archive at Nabble.com.

Re: EntityManager/EntityManagerFactory not injected to CDI bean

Posted by Romain Manni-Bucau <rm...@gmail.com>.
Hi

can be that a jar is not used by cdi cause of anything, can you start with
the system property openejb.cdi.debug=true to check the class is scanned.

Also is your unit a resource local one or jta one?


Romain Manni-Bucau
@rmannibucau <https://twitter.com/rmannibucau> |  Blog
<https://blog-rmannibucau.rhcloud.com> | Old Blog
<http://rmannibucau.wordpress.com> | Github <https://github.com/rmannibucau> |
LinkedIn <https://www.linkedin.com/in/rmannibucau> | JavaEE Factory
<https://javaeefactory-rmannibucau.rhcloud.com>

2017-02-08 13:05 GMT+01:00 Hynek <hy...@gmail.com>:

> Hello all!
>
> I have an issue with injecting EntityManagerFactory (or EntityManager) into
> a CDI managed bean.
>
> My application is structured such that I have a root app.ear containing
> persistence.jar and rest.war. When deployed to TomEE 7.0.2 it initialized
> and starts up OK (no errors in the logs). persistence.jar defines my JPA
> entities, persistence.xml with persistent unit, a service class referencing
> EntityManager and providing business methods to the servlet classes in
> rest.war:
>
> EntityManager is being resolved with a CDI bean below. The motivation is,
> besides to have a bit more flexibility with CDI, to be able to explicitly
> drive the life time of the produced EntityManager instances:
>
>
>
>
> And the service consuming the produced EntityManager:
>
>
> Both classes above are packaged in peristence.jar.
>
>
> Then in rest.war the service is consumed as follows:
>
>
> As I have mentioned the ear deploys OK with no apparent errors. But when a
> request comes to RestServer *the app fails with a NullPointerException
> inside of EntityManagerProducer.newEntityManager() due to emFactory being
> null, it is never set*.
>
> Interesting to note, when I remove the @Inject annotation of Service.em and
> use @PersistentContext directly there, the entity manager is properly
> injected into the Service EJB and the REST request passes OK.
>
> Any help is greatly appreciated!
>
> Thanks,
> Hynek
>
>
>
>
> --
> View this message in context: http://tomee-openejb.979440.
> n4.nabble.com/EntityManager-EntityManagerFactory-not-injected-to-CDI-bean-
> tp4681031.html
> Sent from the TomEE Users mailing list archive at Nabble.com.
>