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

          Hi,
          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()
           {
           super("EmbeddedEjb3TestCase");
           }
          
          
           public static Test suite() throws Exception
           {
           TestSuite suite = new TestSuite();
           suite.addTestSuite(EmbeddedEjb3TestCase.class);
          
          
           // setup test so that embedded JBoss is started/stopped once for all tests here.
           TestSetup wrapper = new TestSetup(suite)
           {
           protected void setUp()
           {
           startupEmbeddedJboss();
           }
          
           protected void tearDown()
           {
           shutdownEmbeddedJboss();
           }
           };
          
           return wrapper;
           }
          
           public static void startupEmbeddedJboss()
           {
           EJB3StandaloneBootstrap.boot(null);
           EJB3StandaloneBootstrap.scanClasspath();
           }
          
           public static void shutdownEmbeddedJboss()
           {
           EJB3StandaloneBootstrap.shutdown();
           }
          
           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");
          
           tm.begin();
          
           Customer cust = new Customer();
           cust.setName("Bill");
           em.persist(cust);
          
           assertTrue(cust.getId() > 0);
          
           int id = cust.getId();
          
           System.out.println("created bill in DB with id: " + id);
          
           tm.commit();
          
           tm.begin();
           cust = em.find(Customer.class, id);
           assertNotNull(cust);
           tm.commit();
           }
          
           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>
             <persistence-unit name="foodb">
             <jta-data-source>java/DefaultDS</jta-data-source>
             <properties>
             <property name="jboss.entity.manager.jndi.name" value="java:/EntityManager/foodb"/>
             </properties>
             </persistence-unit>
            </persistence>
            


            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.