1 Reply Latest reply on May 8, 2007 1:21 PM by Rodney Kite

    EntityManagerFactory creation failure EJB 2.1 - EJB 3.0

    Rodney Kite Newbie

      I have a sample application I am using to learn EJB 3.0. I am having a problem accessing EJB 3.0 beans from an EJB 2.1 session bean. I can not get an EntityManagerFactory. I need an EntityManager to perform an EJB 3.0 finder from the 2.1 Session bean. I am using JBoss 4.2 CR2. Using EJB 3.0 session and entity Beans together is not a problem. What else needs to be done?



      Here is the error I am getting from the EntityManagerFactory creation line.



      10:52:50,799 INFO [SettingsFactory] Default entity-mode: pojo
      10:52:50,799 INFO [SettingsFactory] Named query checking : enabled
      10:52:50,799 INFO [SessionFactoryImpl] building session factory
      10:52:50,815 INFO [SessionFactoryObjectFactory] Not binding factory to JNDI, no
      JNDI name configured
      10:52:50,815 ERROR [LogInterceptor] RuntimeException in method: public abstract
      java.util.TreeSet com.ejb.UserManager.retrieveMessages(java.lang.String) throws
      java.lang.Exception,java.rmi.RemoteException:
      javax.persistence.PersistenceException: org.hibernate.HibernateException: The ch
      osen transaction strategy requires access to the JTA TransactionManager
      at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Con
      figuration.java:720)






      // Example of getting EJB 3.0 beans from EJB 2.1 Session Bean.
      public List findPosts(String userLogin) throws Exception
      {
      HashMap map = new HashMap();
      InitialContext initialContext = new InitialContext();
      EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("imEjb30", map);
      EntityManager entityManager = entityManagerFactory.createEntityManager();
      entityManagerFactory.close();

      Query query = entityManager.createNamedQuery("findPosts");
      query.setParameter(0, userLogin);
      List list = query.setMaxResults(10).setFirstResult(0).getResultList();

      return(list);
      }

        • 1. Re: EntityManagerFactory creation failure EJB 2.1 - EJB 3.0
          Rodney Kite Newbie

          I ended up using the InitialContext to lookup the EntityManagerFactory. Not sure if this is the correct way or not but it works. Below is the new code.



          // Example of getting EJB 3.0 beans from EJB 2.1 Session Bean.
          public List findPosts(String userLogin) throws Exception
          {
          InitialContext initialContext = new InitialContext();
          Object obj = initialContext.lookup("java:/imEjb30Factory");
          EntityManagerFactory entityManagerFactory = (EntityManagerFactory)obj;
          EntityManager entityManager = entityManagerFactory.createEntityManager();

          Query query = entityManager.createNamedQuery("findPosts");
          query.setParameter(1, userLogin);
          List list = query.setMaxResults(10).setFirstResult(0).getResultList();

          return(list);
          }