Skip to main content

@PersistenceContext ignored in library jar classes?

10 replies [Last post]
bergler
Offline
Joined: 2003-06-11
Points: 0

I have built a minimal .ear containing an ejb.jar on top level and a library.jar in the lib folder of the .ear.
The library.jar contains a META-INF/persistence.xml, both ejb.jar and library.jar contain empty META-INF/beans.xml. @Injection works fine in both jars.

Within the library.jar I try to inject an EntityManager with
@PersistenceContext(unitName="xxx") EntityManager em;

I think at deploy time this annotation is regarded by glassfish, because an incorrect unitName leads to an appropriate deployment error.

At runtime, alas, the member is allways null.

The same annotation within the ejb.jar works fine.

Could this be a bug in glassfish? I'm using 3.1.2.2 and have tried it even with 3.1.0, same effect.

thank you for your help
/roland

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
mvatkina
Offline
Joined: 2005-04-04
Points: 0

In which component are you injecting the PC? If you use CDI, do you have beans.xml in that library's jar META-INF dir?

bergler
Offline
Joined: 2003-06-11
Points: 0

I'm injecting into a simple pojo managed bean living in a .jar in the ear's lib/-folder. beans.xml exists and injection works(other then @PersistenceContext). If I add some @Inject to the pojo it is done correctly. Jus So I guess CDI is set up correctly.

If I give the incorrect name for the persistence unit, glassfish complains and will not deploy. If I correct the name, glassfish will deploy but inject nothing (null).

mvatkina
Offline
Joined: 2005-04-04
Points: 0

Seems like a bug to me.

-marina

forums@java.net wrote:
> I'm injecting into a simple pojo managed bean living in a .jar in the
> ear's
> lib/-folder. beans.xml exists and injection works(other then
> @PersistenceContext). If I add some @Inject to the pojo it is done
> correctly.
> Jus So I guess CDI is set up correctly. If I give the incorrect name
> for the
> persistence unit, glassfish complains and will not deploy. If I
> correct the
> name, glassfish will deploy but inject nothing (null).
>
> --
>
> [Message sent by forum member 'bergler']
>
> View Post: http://forums.java.net/node/889633
>
>

ljnelson
Offline
Joined: 2003-08-04
Points: 0

On Tue, Aug 28, 2012 at 8:07 AM, wrote:

> Within the library.jar I try to inject an EntityManager with
> @PersistenceContext(unitName="**xxx") EntityManager em;

You can't do this. You may use @PersistenceContext inside an EJB but not a
library jar.

Best,
Laird

--
http://about.me/lairdnelson

bergler
Offline
Joined: 2003-06-11
Points: 0

Could not find anything on this in the jee, ejb, webbeans or jpa specification.

The Weld documentation says:

"All managed beans may take advantage of Java EE component environment injection using @Resource, @EJB, @PersistenceContext, @PeristenceUnit and @WebServiceRef."

regards
/roland

mgainty
Offline
Joined: 2004-05-21
Points: 0

Annotated pojo must be defined as in beans.xml
@PersistenceContext requires a META-INF/persistence.xml
If there is still a failure try @PersistenceContext(unitName="name")

also make sure annotations-api*.jar is ON CLASPATH
http://www.jarfinder.com/index.php/jars/versionInfo/4584

http://stackoverflow.com/questions/5029571/how-to-inject-persistencecont...
Martin
______________________________________________
Verzicht und Vertraulichkeitanmerkung/Note de déni et de confidentialité

Diese Nachricht ist vertraulich. Sollten Sie nicht der vorgesehene Empfaenger sein, so bitten wir hoeflich um eine Mitteilung. Jede unbefugte Weiterleitung oder Fertigung einer Kopie ist unzulaessig. Diese Nachricht dient lediglich dem Austausch von Informationen und entfaltet keine rechtliche Bindungswirkung. Aufgrund der leichten Manipulierbarkeit von E-Mails koennen wir keine Haftung fuer den Inhalt uebernehmen.
Ce message est confidentiel et peut être privilégié. Si vous n'êtes pas le destinataire prévu, nous te demandons avec bonté que pour satisfaire informez l'expéditeur. N'importe quelle diffusion non autorisée ou la copie de ceci est interdite. Ce message sert à l'information seulement et n'aura pas n'importe quel effet légalement obligatoire. Étant donné que les email peuvent facilement être sujets à la manipulation, nous ne pouvons accepter aucune responsabilité pour le contenu fourni.

> To: users@glassfish.java.net
> Subject: Re: @PersistenceContext ignored in library jar classes?
> From: forums@java.net
> Date: Tue, 28 Aug 2012 12:48:57 -0500
>
> Could not find anything on this in the jee, ejb, webbeans or jpa
> specification. The Weld documentation [1] says: "All managed beans may take
> advantage of Java EE component environment injection using @Resource, @EJB,
> @PersistenceContext, @PeristenceUnit and @WebServiceRef." regards /roland
>
> [1]
> https://docs.jboss.org/weld/reference/1.0.1-Final/en-US/html/ee.html#d0e...
>
> --
>
> [Message sent by forum member 'bergler']
>
> View Post: http://forums.java.net/node/889633
>
>

bergler
Offline
Joined: 2003-06-11
Points: 0

> Annotated pojo must be defined as in beans.xml
check

>@PersistenceContext requires a META-INF/persistence.xml
check

>If there is still a failure try @PersistenceContext(unitName="name")
yes

>also make sure annotations-api*.jar is ON CLASPATH

tried it, didn't change anything. annotation-classes should be on classpath anyway, they are part of JEE6 api

regards
/roland

ljnelson
Offline
Joined: 2003-08-04
Points: 0

On Tue, Aug 28, 2012 at 10:48 AM, wrote:

> Could not find anything on this in the jee, ejb, webbeans or jpa
> specification. The Weld documentation [1] says: "All managed beans may take
> advantage of Java EE component environment injection using @Resource, @EJB,
> @PersistenceContext, @PeristenceUnit and @WebServiceRef."
>

I stand corrected; you are quite right. I've held this misconception for
quite a while. Thanks.

As a (gross) workaround, have you tried annotating your library jar with
@ManagedBean? I know this is a horrible-sounding idea, but it was the only
way we could get certain kinds of injection to work with JAX-RS and
GlassFish (we had library jars in an ear file's lib directory and an empty
JAX-RS application deployed as a .war; injection wouldn't work on the
library jars until they were annotated with @ManagedBean, and then at least
we could use @EJB injection inside them).

Best,
Laird

--
http://about.me/lairdnelson

bergler
Offline
Joined: 2003-06-11
Points: 0

ljnelson wrote:
On Tue, Aug 28, 2012 at 10:48 AM, wrote:

As a (gross) workaround, have you tried annotating your library jar with @ManagedBean?

I tried it, but it did not change anything.

How can it be nobody noticed this until now? I tried it with gf 3.0 - gf-3.1.2.2 an it's the same.
I have the suspicion I'm doing something wrong.

As a workaround for now, I'm taking the EntityManager from jndi, which means I have to annotate all my ejbs with @PersistenceContext. argggh. There seems to be no better way... I even tried "@Produces @PersistenceContext" in my ejb.jar and @Inject this into my jar, but this does not work (because of class loading)

I will compile a bug report on this.

HankCa
Offline
Joined: 2013-09-18
Points: 0

I've just solved the same problem. All was fine in my EAR application until I cleaned up the architecture and separated the CRUD services into a separate jar to the Entities and persistence unit.

I followed Piotr Norwicki in http://piotrnowicki.com/2012/11/types-of-entitymanagers-application-mana... and created my EntityManager via EntityManagerFactory:

import javax.ejb.Stateless;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Disposes;
import javax.enterprise.inject.Produces;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;

@Stateless
public class EntityProducer {

@PersistenceUnit(name = "esa2-pu")
EntityManagerFactory emf;

@Produces
@RequestScoped
public EntityManager createEntityManager() {
return emf.createEntityManager();
}

public void destroyEntityManager(@Disposes EntityManager em) {

if (em.isOpen()) {
em.close();
}
}
}

I needed to add the @Stateless where Piotr doesn't have it. I'm not sure why and still trying to work that out.