I have 2 ejb-module in my project, ejb-module1 and ejb-module2. ejb-module1 contain entity clases and persistence unit, there is a ejb with a Entity Manager like this:
@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;
}
in ejb-module2 i have other ejb that need use entity manager from ejb-module1, i try with this,
String ejbql = "SELECT e from CtEmpresaCliente e ORDER BY e.idCliente ASC";
Query query = this.erpTools.getEm().createQuery(ejbql);
empresaClientes = query.getResultList();
but send this exception:
"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.
note: Using Netbeans
, JPA
, JEE6
, EJB 3.1
Calls to EJBs that are in different modules are similar to remote invocations in the sense that they use pass by value semantics and undergo serialization / de-serialization. In this case the network is not used, but all other aspects of the remote invocations are still happening.
What this means for you is that even though you get a non null EntityManager
from another EJB module, it is serialized / de-serialized and by the time it makes it to the other EJB module it no longer refers to a valid persistence context (since it does not exist in the calling EJB module).