1 Reply Latest reply on Mar 8, 2006 8:44 AM by Jens Elkner

    Problems with getting EntityManagerFactory via JNDI

    Nikolaus Gerstmayr Newbie

      Hi all,

      Scenario/Problem: Client needs to get an Entitymanager / Entitymangerfactory via JNDI.

      Studying docs/wikis/forum I came up with this:

       try {
       Context context = new InitialContext();
      EntityManagerFactory emf = (EntityManagerFactory) context.lookup("java:/EntityManagerFactory");
      logger.debug("emf "+emf);
      //manager = emf.createEntityManager();
      


      persistence.xml:

       <persistence-unit name="mos">
       <provider>org.hibernate.ejb.HibernatePersistence</provider>
       <jta-data-source>java:/mystDS</jta-data-source>
       <properties>
       <!-- JNDI name for EntityManager+Factory (we need this for non container managed access to this PU -->
       <property name="jboss.entity.manager.factory.jndi.name" value="java:/EntityManagerFactory"/>
       <property name="jboss.entity.manager.jndi.name" value="java:/EntityManager"/>
       <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
       <property name="hibernate.hbm2ddl.auto" value="update"/>
       </persistence-unit>
      </persistence>
      


      Problem: It always returns null :(

      I've also tried it that way:

       EntityManagerFactory emf2 =Persistence.createEntityManagerFactory("mos");
      emf2.cerate
      //manager = emf2.createEntityManager();
      


      (and putting persistence.xml to META-INF)

      but this gives this exception:

      Exception in thread "main" javax.persistence.PersistenceException: java.lang.NullPointerException
       at org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:173)
       at org.hibernate.ejb.HibernatePersistence.createEntityManagerFactory(HibernatePersistence.java:103)
       at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:37)
       at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:27)
       at com.ttech.myst.client.test.Test2.main(Test2.java:68)
      Caused by: java.lang.NullPointerException
       at org.hibernate.ejb.packaging.PersistenceXmlLoader.deploy(PersistenceXmlLoader.java:68)
       at org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:139)
       ... 4 more
      



      For stateless/full beans getting the entitymanager
      via injection works wunderful, (via @PersistenceContext(unitName="..."))
      and I'm awre that calling beans in hte client rather than putting the bussinesslayer/EJB3 queries directly into the client is suboptimal, but I cant avoid it in my case :-((( (long story)


      Any help would be greatly appriciated :)
      thx in advance, nick