1 Reply Latest reply on Dec 2, 2010 11:42 AM by Assem Bardi

    How to get entity Manager of Seam without @In and Component.getInstance("entityManager")

    Assem Bardi Newbie

      Hi all,
      i have a long running thread and i need to use the EntityManagaer of my seam project and i dont want use @In, neihter Component.getInstance(entityManager)


      i used this code to lookup the entity Manager Factory and is worked fine :



      InitialContext  ctx = new InitialContext();
      EntityManagerFactory factory = (EntityManagerFactory)ctx.lookup("java:/DBEntityManagerFactory");
      EntityManager em = factory.createEntityManager();
      System.out.println("Factory is "+factory);
      System.out.println("EntityManager is "+em);



      ok this is working fine but the problem is that am creating a new EntityManager instead of using the one used by my project which is seam one.


      Can i get the EntityManager of my Seam Project from the context by a lookup as i did for my EntityManagerFactory?
      is there any other solution?



      Thanks for helping me.


      Assem


        • 1. Re: How to get entity Manager of Seam without @In and Component.getInstance("entityManager")
          Assem Bardi Newbie

          now am doing like this :


          i added :


             <persistence-context-ref>
            <persistence-context-ref-name>persistence/EntityManager</persistence-context-ref-name>
               <persistence-unit-name>DB</persistence-unit-name>
            </persistence-context-ref>


          to web.xml inside web-app tag


          and am getting my entitymanager and a userTransaction like this:


          javax.transaction.UserTransaction tx = (javax.transaction.UserTransaction)ctx.lookup("UserTransaction");
          EntityManager entityManager = (EntityManager) ctx.lookup("java:comp/env/persistence/EntityManager");
          tx.begin();
          entityManager.joinTransaction();
          //Do staff
          tx.commit();





          it seems working. hope that will help someone.
          Thanks to all!
          keep going on Seam thanks!