users@glassfish.java.net

Re: EntityManager Lookup via JNDI in Glassfish

From: Sahoo <Sahoo_at_Sun.COM>
Date: Thu, 19 Mar 2009 10:09:31 +0530

glassfish_at_javadesktop.org wrote:
> I'm looking for a way to get an EntityManager into a POJO running in Glassfish. I'd like to do the lookup via JNDI but can't seem to find a way to get GF to provide an EntityManager.
>
> The app is a collection of EJBs, POJOs, and Entities. There is no web tier.
>
> I've tried this:
>
> try {
> Context ic = (Context) new InitialContext();
> this.em = (EntityManager) ic.lookup("java:comp/env/persistence/em");
>
Depending on the component context in which the POJO is getting called,
you need to define a persistence-context-ref in in that component's
naming context. You can do that by placing a
@PersistenceContext(name="persistence/em", ...) in the EJB class that
calls the POJO.

Sahoo