users@glassfish.java.net

How to access EntityManager of a EJB from a WAR? (without using facade pattern)

From: Xavier Callejas <xavier_at_wflogistics.com>
Date: Wed, 3 Feb 2010 11:09:36 -0600

Hi,

I have a EJB jar containing entities from database, the jar includes its
persistence.xml (lets call it model-ejb.jar).

In model-ejb.jar I have a session bean (with remote interface) with a method
to make public the entity manager:

...

// Stateless
// public class MyModelBean implements MyModelBeanRemote {

...

@PersistenceContext private EntityManager em;

public EntityManager getEntityManager() {
        return em;
}

...

I have a web application that includes this model-ejb.jar in the classpath,
and I would like to access its EntityManager instance.

...
@EJB MyModelBeanRemote mmbr;

public void test() {
        EntityManager em = mmbr.getEntityManager();
        ...
...

The problem is that "mmbr.getEntityManager()" returns null.


I only can use the entity manager instance inside Model-ejb.jar, but not
ourside using a remote session bean (for now I can only wrapped it in a facade
pattern to be able to use it in the .war).

what am I doing wrong?

Thank you in advance.