persistence@glassfish.java.net

Re: Use same persistence unit in ejb and web tier

From: Benjamin Graf <Benjamin.Graf_at_gmx.net>
Date: Tue, 24 Oct 2006 22:27:31 +0200

-----BEGIN PGP SIGNED MESSAGE-----
Hash: RIPEMD160

Hmm,

your response makes be surprised. I used that code (generated by NetBeans for
this special problem) while working with a persistence unit scope in the web
application. But the same code doesn't work if the scope is in the enterprise
application? As far as I know the Persistence class should be used in the J2SE
environment so I'm not sure I can also use it in the glassfish environment.

Best regards,
Benjamin

Ian Evans wrote:
> Benjamin Graf <Benjamin.Graf_at_gmx.net> wrote:
>> one problem is still there. How can I access this global defined
>> persistence unit. I tried an lookup,
>>
>> Context ctx = new InitialContext();
>> em = (EntityManager)
>> ctx.lookup("java:comp/env/persistence/LogicalName");
>> persistence/LogicalName registered in web.xml
>>
>> but the EntityManager was null.
>
> In a web application that can't do resource injection, you use an
> EntityManagerFactory to create entity managers. You can't look up entity
> managers through JNDI.
>
> EntityManagerFactory emf =
> Persistence.createEntityManagerFactory("PersistenceUnitName");
> EntityManager em = emf.createEntityManager();
>
> If your web app is a servlet or has a JSF backing bean, you can inject
> the persistence unit just as you would in a session bean.
>
> I suggest you read the "Persistence in the Web Tier" chapter and look at
> the bookstore examples in the Java EE Tutorial. They demonstrate
> different strategies in web apps. For example, bookstore2 uses a servlet
> context listener to inject the entity manager factory.
>
> -i
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (MingW32)

iD8DBQFFPncz+oSgMM7Y9KsRA3EXAKCGoO0QKjXeRPGDiotrLzCDk8VpnQCePoOs
XPT8hjuHuCcoc8MZTXFg/zE=
=mES3
-----END PGP SIGNATURE-----