简体   繁体   English

使用CDI注入PersistenceContext

[英]Inject PersistenceContext with CDI

Currently, I'm using PersistenceContext to inject an EntityManager. 目前,我正在使用PersistenceContext来注入EntityManager。 The EM is injected perfectly. EM完美注入。

@Stateless
public StatelessSessionBean implements StatelessSessionBeanLocal {

    @PersistenceContext(unitName = "MyPersistenceUnit")
    private EntityManager em;

    @Override
    public Collection<MyObject> getAllObjects(){
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriqQuery<MyObject> query = cb.createQuery(MyObject.class);
        query.from(MyObject);
        return em.createQuery(query).getResultList();
    }
}

Now I try to decorate the bean, and suddenly the em doesn't get injected. 现在我尝试装饰豆子,突然间em没有被注射。 I get a NullPointerException. 我得到一个NullPointerException。

@Decorator
public StatelessSessionBeanDecorator implements StatelessSessionBeanLocal {

    @Inject
    @Delegate
    @Any
    StatelessSessionBeanLocal sb

    @Override
    public Collection<MyObject> getAllObjects(){
        System.out.println("Decorated method!");
        return sb.getAllObjects();
    }
}

I know EJB and CDI are 2 completely different managers, so the one doesn't know about the other. 我知道EJB和CDI是两个完全不同的管理器,所以那个人不了解另一个。 I'm expecting that @PersistenceContext is an EJB injection point, while @Inject is a CDI one. 我期望@PersistenceContext是一个EJB注入点,而@Inject是一个CDI注入点。 What should I do to solve this and get the EntityManager to be injected like it should? 我该怎么做才能解决这个问题并让EntityManager像它应该注入一样?

The best practice for persistence context and CDI is to make them CDI bean to avoid these kind of issue. 持久化上下文和CDI的最佳实践是使它们成为CDI bean以避免这类问题。

public class MyProducers {
    @Produces
    @PersistenceContext(unitName = "MyPersistenceUnit")
    private EntityManager em;
}

After that you'll be able to inject the EntityManager in CDI way. 之后,您将能够以CDI方式注入EntityManager Taking your EJB it'll be : 拿你的EJB它将是:

@Stateless
public StatelessSessionBean implements StatelessSessionBeanLocal {

    @Inject
    private EntityManager em;

    @Override
    public Collection<MyObject> getAllObjects(){
        CriteriaBuilder cb = em.getCriteriaBuilder();
        CriteriqQuery<MyObject> query = cb.createQuery(MyObject.class);
        query.from(MyObject);
        return em.createQuery(query).getResultList();
    }
}

This way, you'll be able to decorate your CDI bean with no issue. 这样,您就可以毫无问题地装饰CDI bean。

If you have multiple EntityManagers you can use CDI qualifiers to distinguish them 如果您有多个EntityManagers ,则可以使用CDI限定符来区分它们

@PersistenceContext is an EJB injection point, while @Inject is a CDI one @PersistenceContext是一个EJB注入点,而@Inject是一个CDI注入点

Actually, no. 实际上,没有。 @PersistenceContext annotation can be used in CDI and is not connected with EJB. @PersistenceContext注释可以在CDI中使用,并且不与EJB连接。 You can do something like this: 你可以这样做:

@Named
public class EntityDAO {
    @PersistenceContext
    private EntityManager manager;

    ...

}

EJB uses @EJB annotation to inject other EJB, but it can inject any CDI bean or persistence context with same annotations. EJB使用@EJB注释来注入其他EJB,但它可以使用相同的注释注入任何CDI bean或持久性上下文。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM