users@glassfish.java.net

Two EJB modules within one EAR and PersistenceContext issue

From: Witold Szczerba <pljosh.mail_at_gmail.com>
Date: Thu, 12 Jul 2007 00:19:44 +0200

Hi there,
In my company, we are developing enterprise application NB5.5.1 and
Glassfish v2.
Our application contains (among others) two EJB modules, each one
defines persistence context, in "Projects" window it looks more or
less like this:

EAR (Enterprise Application) "SOP"

- SOP-ejb
- - declares persistence-unit name="SOP-ejbPU" transaction-type="JTA"

- SOP-FMAccounting
- - declares persistence-unit name="SOP-FMAccountingPU" transaction-type="JTA"
- - contains SOP-ejb project on its class path

- SOP-app-client (Application Client Container)

First module is a core of our system, it does not depend on any other
module, the SOP-FMAccounting module is a support module that
implements one of "SOP-ejb" local session bean interface.
SOP-FMAccounting depends on SOP-ejb and contains "SOP-ejb" project on
its class path.

The problem is that in SOP-app-client I cannot inject the entity
manager of SOP-ejb to work with/fetch its entities. Here is what I did
in SOP-FMAccounting session bean:

@Stateless
public class SymfoniaExportServiceBean
 implements AccountingExportService {

  @PersistenceContext(unitName="SOP-ejbPU")
  private EntityManager emSop;

  @PersistenceContext(unitName="SOP-FMAccountingPU")
  private EntityManager em;

  public void process(Transaction transaction) {
    //here I need to make a query on SOP-ejb entities
  }
}

However, when it is like this, Glassfish refuses to deploy that EAR, because of:
 Could not resolve a persistence unit corresponding to the
persistence-context-ref-name
[abcxyz.sop.fm.accounting.SymfoniaExportServiceBean/emSop] in the
scope of the module called [].

In "6.2.2 Persistence Unit Scope" of JSR 220 Specification, they say:
"A persistence unit that is defined at the level of an EJB-JAR,WAR, or
application client jar is scoped to that EJB-JAR, WAR, or application
jar respectively and is visible to the components defined in that jar
or war."

Does it means, I cannot access SOP-ejb's entity manager from within
SOP-FMAccounting even though the latter EJB has the first one on its
classpath?

If so, it looks like I have only two options:
 /* I tried this, but it did not work */ move SOP-ejb's
persistance.xml into the top-level EAR, but I do not want to do this,
because I would have to manually declare every entity bean (when it is
in SOP-ejb, I can include all at once). Moreover, when there are
@Entity declared in a module that does not contain persistence.xml,
NetBeans 5.5.1 complains and underlines every @Entity declaration in
project (btw: is that correct?)

There is another option as well, I tried it already just a while ago:
in SOP-FMAccounting I can define second PU, point it at the same JDBC
data source and declare all the entities from SOP-ejb which I want to
use from within FMAccounting EJB. I tried it, but there is one thing:
when I create/fetch entity in SOP-ejb module (Transaction entity in my
case) and pass it to SOP-FMAccounting (using @Local interface) and
invoke
emSop.contains(transaction)
i get >false< which shows that there are no SOP-ejb's entities I can
safely change, because there are now two independent entity managers
in the system that operate on the same set of entities and datasource
:/

Am I missing something? What are yours suggestion for my case? Maybe I
should change local interface, and add EntityManager as a second
parameter:

void process(Transaction t, EntityManager emSop);

so I could just pass the EntityManager instance from SOP-ejb into
SOP-FMAccounting, so the latter could do whatever it wants with it?
What do you think?