如何在运行时根据条件使用不同的EntityManager?

时间:2019-03-05 09:15:49

标签: jpa ejb cdi

我的配置如下:

抽象类:

public abstract class AbstractFacade<T> {

    private Class<T> entityClass;

    public AbstractFacade(Class<T> entityClass) {
        this.entityClass = entityClass;
    }

    protected abstract EntityManager getEntityManager();

    public T find(Object id) {
        return getEntityManager().find(entityClass, id);
    }

    // other methods create(T), edit(T), ...

扩展抽象类的Ejb(我还有许多其他EJB和大约12个不同的持久性单元):

@Stateless
public class FilesDao extends AbstractFacade<Files> {

    @PersistenceContext(unitName = "myfirstPU")
    private EntityManager firstEm;

    @PersistenceContext(unitName = "mysecondPU")
    private EntityManager secondEm;

    // i have more than two persistenceUnit ... 

    @Override
    protected EntityManager getEntityManager() {
         return firstEm; // or secondEm based on condition
    }

    public FilesDao() {
         super(Files.class);
    }

    public Files findByFileref(String inFileRef) {
         try {
             Query q = firstEm.createNamedQuery("Files.findByFileref"); // or secondEm based on condition
             q.setParameter("fileref", inFileRef);
             Files file = (Files) q.getSingleResult();
             return file;
        } catch (NoResultException e) {
             return null;
        }
    }

我想这样使用FilesDao:

@Stateless
@LocalBean
public class FileBusiness {

    @EJB
    FilesDao fileDao;

    public void myMethod(){
    if(condition1){
         //use the FileDao with the EnityManager **firstEm**
    }
    else if(condition2){
        //use the FileDao with the EnityManager **secondtEm**
    }
    ...

}

有没有办法实现这一目标?

我读到有关通过Produce方法使用CDI的信息。

1 个答案:

答案 0 :(得分:0)

CDI生产者并不难,请参见以下示例。

以下类是CDI-Qualifier批注,用于区分实现。

@Retention(RetentionPolicy.RUNTIME)
@Target({ TYPE, METHOD, FIELD, PARAMETER, CONSTRUCTOR }) 
@Qualifier 
public @interface MyfirstPUQualifier {  }

@Retention(RetentionPolicy.RUNTIME)
@Target({ TYPE, METHOD, FIELD, PARAMETER, CONSTRUCTOR }) 
@Qualifier 
public @interface MysecondPUQualifier {  }

以下是一个CDI-Bean,该CDI-Bean注入不同的EntityManagers并实现生产者方法,从而使两个EntityManagers可供CDI使用,并通过CDI-Qualifiers进行区分。

@ApplicationScoped
class MYProducer {
    @PersistenceContext(unitName = "myfirstPU")
    private EntityManager firstEm;

    @PersistenceContext(unitName = "mysecondPU")
    private EntityManager secondEm;

    @Produces
    @RequestScoped
    @MyfirstPUQualifier
    public EntityManager produceMyfirstPU(){
        return firstEm;
    }


    @Produces
    @RequestScoped
    @MysecondPUQualifier
    public EntityManager produceMysecondPU(){
        return secondEm;
    }
}

以下显示了同时注入两个EntityManager的CDI-Bean。可以将其提取到抽象基类中,因为其他DAO也可能需要。

@Stateless
public class FileDao {

    @Inject
    @MyfirstPUQualifier
    private EntityManager emFirst;

    @Inject
    @MysecondPUQualifier
    private EntityManager emSecond;

    public void myDaoMEthod(){
        final EntityManager em = getEntityManager();
        ...
    }

    private EntityManager getEntityManager(){
        if(condition1){
            return emFirst;
        }
        else if(condition2){
            return emSecond;
        }
    }
}

没有任何注意事项,就不能使用FileDao bean,因为无论如何都不应该在此bean中决定

@Stateless
@LocalBean
public class FileBusiness {

    @EJB
    FilesDao fileDao;

    public void myMethod(){
        // FileDao will decide what entity manager to use
        fileDao.doStruff();
    }
}