Getting a reference to EntityManager in Java EE applications using CDI

后端 未结 4 1495
无人及你
无人及你 2020-12-04 12:28

I\'m using Java EE 7. I would like to know what is the proper way to inject a JPA EntityManager into an application scoped CDI bean. You can\'t

4条回答
  •  悲&欢浪女
    2020-12-04 13:29

    I understand your problem. but that is not a real one. Do not get messed up with the CDI declared scope of a containing class, that will propagate the scope of the attributes expect those that use @Inject'ion!

    The @Inject'ed will compute their reference in depencency of the CDI-declaration of the implementation class. So you might have Applicationscoped class with a @Inject EntityManager em inside, but each controlflow will find its own em-transaction reference to a disjount em-object, because of the EntityManager CDI declaration of the implementation class behind.

    The wrong thing of your code is, that you provide an inner getEntityManager() access method. Do not pass Injected object, if you need one, simply @Inject it .

提交回复
热议问题