繁体   English   中英

如何从其他EJBModule将EntityManager注入到EJB

[英]how inject entityManager to EJB from other EJBModule

我的项目中有2个ejb-module,ejb-module1和ejb-module2。 ejb-module1包含实体分类和持久性单元,有一个带有实体管理器的ejb,如下所示:

@Stateful
public class ErpTools implements ErpToolsLocal {
@PersistenceContext(unitName = "erp-ejbPU")
private EntityManager em;


public EntityManager getEm() {
    return em;
}

public void setEm(EntityManager em) {
    this.em = em;
}

在ejb-module2中,我还有其他ejb需要使用ejb-module1中的实体管理器,我尝试使用此方法,

  String ejbql = "SELECT e from CtEmpresaCliente e ORDER BY e.idCliente ASC";        
  Query query = this.erpTools.getEm().createQuery(ejbql);
  empresaClientes = query.getResultList();

但发送此异常:

"Unable to retrieve EntityManagerFactory for unitName erp-ejbPU"
debuging in this point Query query = this.erpTools.getEm().createQuery(ejbql);
this.erpTools.getEm() is not null.

注意:使用NetbeansJPAJEE6EJB 3.1

在不同模块中的EJB调用与远程调用类似,因为它们使用按值传递语义并进行序列化/反序列化。 在这种情况下,不使用网络,但是远程调用的所有其他方面仍在发生。

这对您意味着什么,即使您从另一个EJB模块获得了一个非null的EntityManager ,它也会被序列化/反序列化,并且在将其传递到另一个EJB模块时,它不再引用有效的持久性上下文(因为它在调用EJB模块中不存在)。

暂无
暂无

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

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