users@glassfish.java.net

Two EJB modules inside one EAR and entity manager

From: Witold Szczerba <pljosh.mail_at_gmail.com>
Date: Wed, 10 Oct 2007 23:04:23 +0200

Hi there,
in my company, we are developing an enterprise application (EAR).
Right now, almost all the functionality is inside one EJB module which
we call Core Module.

During development, I noticed that in the core module, there are few
services very specific to the current customer's business logic and
this particular business conditions can change from time to time. So,
for me it looks like the core module is too much internally coupled,
so I would like to split my EJB module, so the Core would be decoupled
from those very specific conditions.

So I created another module, lets call it CompanyX EJB Module which
will be like a plug-in for the Core, it is dependent on the Core
module's API, extending it functionality. In the future we can create
CompanyY EJB Module or make changes in the CompanyX module without
touching the Core.

I have no problem accessing Core's services from within CompanyX
services, simple
@EJB SomeCoreServiceLocal
works great, but I _cannot_ access Core's entity manager.

I have tried something like this:
@PersistenceContext(unitName="corePU")
private EntityManager em;

But it does not work, I have no idea how to inject the entity manager
of Core EJB into CompanyX EJB service.

Once I've heard suggestions that I should tear out all my entities +
persistance.xml from Core service and put it inside some jar at the
EAR level but I do not want to decouple core entities from core EJB
module. Moreover if I do that, I would encounter lot of troubles (like
transaction propagation) because CompanyX EJB would create its own EM
context for accessing entities while it is supposed to work like a
plug-in, not as a separate EJB.

So, in short: is it actually possible to access entity manager of one
EJB module from within another EJB module inside the same EAR?

Thanks,
Witold Szczerba