2 Replies Latest reply on Mar 23, 2006 11:23 AM by Serge Emmanuel Pagop

    Lookup EntityManager without declaring @PersistenceContext

    eekboom Newbie

      Hi, I'm trying to switch my database dynamically, much like discussed in http://jboss.org/index.html?module=bb&op=viewtopic&t=65869

      I have defined several persistence units in my persistence.xml.
      My understanding is that I should be able to lookup entity managers without the need to annotate my session bean with @PersistenceContexts({...}):

      RC1 has these global JNDI definitions defined

      java:/EntityManagers/<unit-name> -- Injected EM

      java:/EntityManagerFactories/<unit-name> - Injected EMF

      I can't get this to work. Has something changed for RC5?

        • 1. Re: Lookup EntityManager without declaring @PersistenceConte
          Serge Emmanuel Pagop Newbie

          I get the same problem when I try to run the testCase EmbeddedEjb3TestCase.java comes with the Embedded-EJB3-ALPHA_5 examples.

          public class EmbeddedEjb3TestCase extends TestCase
           public EmbeddedEjb3TestCase()
           public static Test suite() throws Exception
           TestSuite suite = new TestSuite();
           // setup test so that embedded JBoss is started/stopped once for all tests here.
           TestSetup wrapper = new TestSetup(suite)
           protected void setUp()
           protected void tearDown()
           return wrapper;
           public static void startupEmbeddedJboss()
           public static void shutdownEmbeddedJboss()
           public void testEntityManager() throws Exception
           // This is a transactionally aware EntityManager and must be accessed within a JTA transaction
           // Why aren't we using javax.persistence.Persistence? Well, our persistence.xml file uses
           // jta-datasource which means that it is created by the EJB container/embedded JBoss.
           // using javax.persistence.Persistence will just cause us an error
           EntityManager em = (EntityManager) getInitialContext().lookup("java:/custdb");
           // Obtain JBoss transaction
           UserTransaction tm = (UserTransaction) getInitialContext().lookup("UserTransaction");
           Customer cust = new Customer();
           assertTrue(cust.getId() > 0);
           int id = cust.getId();
           System.out.println("created bill in DB with id: " + id);
           cust = em.find(Customer.class, id);
           public static InitialContext getInitialContext() throws Exception
           Hashtable props = getInitialContextProperties();
           return new InitialContext(props);
           private static Hashtable getInitialContextProperties()
           Hashtable props = new Hashtable();
           props.put("java.naming.factory.initial", "org.jnp.interfaces.LocalOnlyContextFactory");
           props.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
           return props;

          Tell me do you have a solution for this problem or how I have to inject EntityManages in the embedded-jboss-*.xml file ?

          Thanks ...

          • 2. Re: Lookup EntityManager without declaring @PersistenceConte
            Serge Emmanuel Pagop Newbie

            Try this:
            your persistence.xml file must look like this

             <persistence-unit name="foodb">
             <property name="jboss.entity.manager.jndi.name" value="java:/EntityManager/foodb"/>

            and then in your code just look up EntityManager like this
            EntityManager em = (EntityManager)getJBossSpecificInitialContext().lookup("java:/EntityManager/foodb");

            I hope this can help u to roll your effort.