users@glassfish.java.net

Re: Dependency injection problems

From: Sahoo <Sahoo_at_Sun.COM>
Date: Thu, 19 Feb 2009 12:32:26 +0530

You can't inject an EntityManager into an EntityListener. EntityListener
is not managed by any of the containers. Ideally, the callback methods
should have passed the persistence context in which the entity is being
managed as an argument, but they don't. I have mentioned that as a
feedback to JPA spec lead, but I am not sure if they incorporated the
feedback or not. You can try the same.

In the meanwhile, what you can do is a JNDI lookup of an entity manager
inside the listener or use Java SE style
Persistence.createEntityManagerFactory to create an emf. If you are only
going to use the listener in the context of the Stateless bean, then you
can use the comp:env namespace of the stateless ejb to lookup the entity
manager. So, try doing this in your code:

    @PrePersist
    public void prePersist(Object object) {
        EntityManager em = (EntityManager) new InitialContext().lookup("comp:env/InsertPackageNameHere.TestServiceBean/em");
        // use the em as a container managed entity manager
        ...
    }


Ponits to note here is the JNDI name you are looking up. Since your
StatelessBean did not specify a name in @PersistenceContext, the default
name is "fully qualified class name/field name." More over, this name is
only valid in the call stack of TestServiceBean. This name won't work in
Java SE or when you are using your entity outside the context of this
EJB. Hence it would have been much better had the callback method passed
an EntityManager as an argument.

Let us know if this works. I am curious to know the outcome.
Thanks,
Sahoo

glassfish_at_javadesktop.org wrote:
> Hi All,
>
> I am attempting to implement some Audit logging into a db of a EJB3 / Java EE 5 application.
>
> I have mapped Listeners to selected Entities where I will insert Auditing records into a db where required.
>
> The methods in my Listener classes that are annotated with @PrePersist, @PreUpdate etc are being triggered ok, however I can't
> get the EntityManager to be injected into the listener.
>
> I have only been able to inject an EntityManager into a EJB Bean class.
>
> I have tried injecting a EntityManagerFactory without success.
> ie. @PersistenceUnit EntityManagerFactory emf;
>
> I have included a code snippet.
>
> Thanks in advance,
> Adam
>
>
> import javax.ejb.Stateless;
> import javax.jws.WebMethod;
> import javax.persistence.EntityManager;
> import javax.persistence.PersistenceContext;
>
> @Stateless(name = "TestServiceBean", mappedName = "TestServiceBean")
> @WebService
> public class TestServiceBean implements TestService {
>
> @Resource
> protected SessionContext ctx;
>
> @PersistenceContext(unitName = "em")
> protected EntityManager em;
>
> public void create() {
>
> MyEntity entity = new MyEntity();
> entity.setValue("foo");
>
> em.persist(entity);
>
> }
> }
>
> @Entity
> @EntityListeners({MyEntityListener.class })
> @Table(schema = "schema_name", name = "my_entity")
> public class MyEntity implements Serializable {
> .
> .
> .
> }
>
> public class MyEntityListener {
>
> @PersistenceContext(unitName = "em")
> EntityManager em;
>
> @PersistenceUnit
> protected EntityManagerFactory emf;
>
> @PrePersist
> public void prePersist(Object object) {
>
> // EntityManager has not been injected. em is null
>
> // Insert Audit rec into db here
> }
>
> }
> [Message sent by forum member 'brabble' (brabble)]
>
> http://forums.java.net/jive/thread.jspa?messageID=332801
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>
>