users@glassfish.java.net

Re: CDI broken between EJB and JPA modules

From: Witold Szczerba <pljosh.mail_at_gmail.com>
Date: Thu, 8 Apr 2010 23:11:54 +0200

2010/4/8 Sahoo <Sahoo_at_sun.com>:
> Hi Dan,
>
> The ear you have attached has separate jar with JPA classes and EJB classes.
> I don't think that would work. So, first you need to merge JPA.jar with
> EJB.jar. I guess you have already tried that unsuccessfully. That's where
> the bug comes in. The persistence deployer in GlassFish is thinking that
> there is no component in the EAR using the persistence-unit, so it does not
> initialize it. Obviously that's a bug. It is not parsing managed beans. As a
> simple work around, do the following in your UserService.java:
>
>
> @LocalBean
> @Stateless
> @WebService
> public class UserService {
> *        // This is present to ensure that persistence-unit is loaded. This
> is a work around to glassfish bug.
> @javax.persistence.PersistenceContext javax.persistence.EntityManager em;
> *
>
> This will cause the persistence unit to be loaded and once that happens, it
> will be successfully injected into the DAO managed bean as well. Give it a
> try. File a bug if this's the case.
>
> Thanks,
> Sahoo
>

Sahoo,
I think you have a valid point here about JPA not being initialized in
EAR if there is no EntityManager injection in any EJB module. I had
same problem once. I had persistence unit, I wanted Toplink just to
create database tables, so there was no @PersistanceUnit annotation in
any enterprise bean and no tables were created, all the JPA hasn't
event started. So, as you suggested, it is enough just to create one
simple EJB artifact, like SLSB, inject entity manager there and live
it all alone, no need to use it - this will be enough for JPA to
initialize, and CDI should have no problem injecting it.

The other thing is, in my EAR, there are many EJB modules and they all
share common persistence unit which is defined in separate JAR, so I
wouldn't merge EJB module and JPA together. Such a merge would make
persistence unit local to that EJB module alone and no other modules
(be it EJBs or WARs) would have access to it.

--
Witold Szczerba