1 Reply Latest reply on Oct 11, 2011 2:57 AM by jaikiran

    Lookup for EntityManager

    vamshi.a

      I have created an EAR which has EJBS and entities. In one of the session bean I am calling Hibernate validator class  which requires EntityManager. In case of JBOSS 6 I have used the Intialcontex.lookp("java:/comp/em) to get the entitymanager.How can I acheive the similar functionality in jboss 7

       

       

         public EntityManager getEntityManager() throws Exception {

       

              InitialContext ctx;

              try {

                  ctx = new InitialContext();

                  return (EntityManager) ctx.lookup("java:comp/env/em");

              }

              catch (NamingException e) {

                  // This is critical internal issue and it is not probably to see in case of proper deployment. So no translation for message applied.

                  String errMsg = "Could not acquire EntityManager from bean context. Reason: " + e.getMessage();

                  log.error(errMsg);

                  throw new Exception(errMsg);

              }

          }

        • 1. Re: Lookup for EntityManager
          jaikiran

          What class is that method from? If it's some container managed class, you can use injection. Or if you are calling some class from a container managed component, then the component can setup the ENC (via deployment descriptor or annotation) and the other class can look it up.