dev@glassfish.java.net

Re: The Issue with getEntityManager on Glassfish

From: Kevin Yuan <kevin.yuan_at_oracle.com>
Date: Thu, 16 Mar 2006 15:19:57 -0500

Thanks a lot, Ken. It worked now, and I will try to use an annotation
instead of JNDI lookup.

Kevin
Kenneth Saks wrote:

> Kevin Yuan wrote:
>
>> 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();
>
> Hi Kevin,
>
> When using InitialContext() within a Java EE component, all component
> dependencies are looked up relative to the component naming
> environment, java:comp/env. So, this should be
>
> ((EntityManagerFactory)ctx.lookup("java:comp/env/persistence/myPersistenceUnit")
>
> You can avoid having to perform an explicit JNDI lookup by using an
> annotation :
>
> private @PersistenceUnit(unitName="ejb30_Advanced")
> EntityManagerFactory emf;
>
> --ken
>
>
>>
>> }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
>
>
>