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 <[email protected]> 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