dev@glassfish.java.net

The Issue with getEntityManager on Glassfish

From: Kevin Yuan <kevin.yuan_at_Oracle.com>
Date: Thu, 16 Mar 2006 14:44:50 -0500

Hello,
I am Oracle employee in Ottawa. I have defined <persistence-unit-ref> in
ejb-jar.xml, just like the following (part of ejb-jar.xml):

    <enterprise-beans>
        <session>
            <ejb-name>ServerSideTestHelper</ejb-name>
            
<home>oracle.toplink.essentials.testing.ejb.testframework.ServerSideTestHelperHome</home>
            
<remote>oracle.toplink.essentials.testing.ejb.testframework.ServerSideTestHelper</remote>
            
<ejb-class>oracle.toplink.essentials.testing.ejb.testframework.ServerSideTestHelperBean</ejb-class>
            <session-type>Stateless</session-type>
            <transaction-type>Container</transaction-type>
            <persistence-unit-ref>
                
<persistence-unit-ref-name>persistence/myPersistenceUnit</persistence-unit-ref-name>
                
<persistence-unit-name>ejb30_Advanced</persistence-unit-name>
            </persistence-unit-ref>
        </session>
    </enterprise-beans>

But when I try getEntityManager using this persistence-unit-ref-name to
lookup, I got *javax.naming.NameNotFoundException*. The following are
segment of my code:

      public EntityManager getEntityManager(){
        EntityManager em;
        if (this.entityManager == null){
            try{
                Context ctx = new InitialContext();
                 this.entityManager =
((EntityManagerFactory)ctx.lookup("persistence/myPersistenceUnit")).getEntityManager();
            }catch (NamingException ex){
                throw new TestException("Unable to look Up EnityManager
:" + ex.toString());
            }
        }
        return this.entityManager;
    }

Would you mind to point me if I could getEntityManager like the above?
Thanks a lot,

Kevin