users@glassfish.java.net

Re: Resource Injection

From: Craig McClanahan <Craig.McClanahan_at_Sun.COM>
Date: Tue, 06 Jun 2006 10:07:48 -0700

Jason Lee wrote:
> I'm trying to get my container (Glassfish) to inject my persistence unit
> into a faces-managed bean, but I'm not having much luck. It is my
> understanding that this should be possible, but I don't see it
> happening. Here's my persistence.xml:
>
> <persistence xmlns="http://java.sun.com/xml/ns/persistence"
> version="1.0">
> <persistence-unit name="em">
> <jta-data-source>jdbc/Chronos</jta-data-source>
> </persistence-unit>
> </persistence>
>
> I package that with the annotated entity classes in a jar in the root of
> my .ear. Here's the relevant portion of my Faces-managed bean:
>
> @PersistenceUnit
> private EntityManagerFactory emf;
> private EntityManager em;
>
> And, just to be safe, I put this in my method body (as opposed a ctor,
> for example):
>
> em = emf.createEntityManager();
>
> The instance var emf, though, is null, so I get a NullPointerException.
>
> Am I doing something wrong? Is what I'm trying to do even possible?
> I've hit a wall with it. :(
>
>
For what it's worth, it works fine for me (using either the 5.5 preview
or 5.5 beta). The thing I'm doing different, though, is injecting an
EntityManager instead of an EntityManagerFactory (because I'm injecting
into a request scope backing bean). You might try that instead.

    @PersistenceUnit
    private EntityManager em;

I'd also scan the server's log file to see if any exceptions are getting
recorded -- might give a clue to why the injection did not work.
> Thanks!
>
> --
> Jason Lee
> Programmer/Analyst
>
>
Craig McClanahan