Hi,

IIRC, you're doing something like so:

@Entity
public class Foo {
    @PersistenceContext private EntityManager em;
    ...
}

The Java EE spec's support for resource injection does not apply for
entity types. So, from an EJB standpoint, you can only use resource
injection in session beans and MDBs. You're getting the NPE because your
EJB container ignores the @PersistenceContext annotation on your entity.
(Note that OpenJPA doesn't do anything at all with @PersistenceContext
annotations.)

If you were using a session bean, then there are two possible answers to
your question:

1. If you are managing your own transactions using JTA and bean-managed
transactions, then you'd be in good shape using @PersistenceContext.

2. If you are managing your own transactions using the JPA
EntityTransaction API, then you cannot use @PersistenceContext to inject
an EM, but instead must use @PersistenceUnit to inject an
EntityManagerFactory (or some other EMF lookup means), and do your own
lifecycle management.

-Patrick

-- 
Patrick Linskey
BEA Systems, Inc. 

_______________________________________________________________________
Notice:  This email message, together with any attachments, may contain
information  of  BEA Systems,  Inc.,  its subsidiaries  and  affiliated
entities,  that may be confidential,  proprietary,  copyrighted  and/or
legally privileged, and is intended solely for the use of the individual
or entity named in this message. If you are not the intended recipient,
and have received this message in error, please immediately return this
by email and then delete it. 

> -----Original Message-----
> From: Matthieu Riou [mailto:[EMAIL PROTECTED] 
> Sent: Friday, March 02, 2007 8:32 AM
> To: open-jpa-dev@incubator.apache.org
> Subject: Entity manager injection
> 
> Hi,
> 
> For ODE we're managing our transactions ourselves. We start 
> them and commit
> them explicitly. By the same token we create the EntityManagerFactory
> ourselves. Is there a possibility to use the EntityManager 
> injection in the
> persistent classes in this context or do we have to implement our own
> ThreadLocal based thingy to have the EM available in our 
> persistent classes
> using it?
> 
> I've tried doing something like:
> 
> 1. Adding "@PersistenceContext  private EntityManager _em;" 
> in a persistence
> class.
> 2. Load the persistence class from an EntityManager created with the
> factory.
> 3. Use _em in my persistent class
>        => NullPointerException
> 
> Thanks,
> Matthieu
> 

Reply via email to