From open-jpa-dev-return-2615-apmail-incubator-open-jpa-dev-archive=incubator.apache.org@incubator.apache.org Mon Mar 05 16:28:38 2007 Return-Path: Delivered-To: apmail-incubator-open-jpa-dev-archive@locus.apache.org Received: (qmail 77194 invoked from network); 5 Mar 2007 16:28:37 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 5 Mar 2007 16:28:37 -0000 Received: (qmail 84390 invoked by uid 500); 5 Mar 2007 16:28:46 -0000 Delivered-To: apmail-incubator-open-jpa-dev-archive@incubator.apache.org Received: (qmail 84374 invoked by uid 500); 5 Mar 2007 16:28:45 -0000 Mailing-List: contact open-jpa-dev-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: open-jpa-dev@incubator.apache.org Delivered-To: mailing list open-jpa-dev@incubator.apache.org Received: (qmail 84365 invoked by uid 99); 5 Mar 2007 16:28:45 -0000 Received: from herse.apache.org (HELO herse.apache.org) (140.211.11.133) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 05 Mar 2007 08:28:45 -0800 X-ASF-Spam-Status: No, hits=-0.0 required=10.0 tests=SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (herse.apache.org: domain of plinskey@bea.com designates 66.248.192.21 as permitted sender) Received: from [66.248.192.21] (HELO usremg01.bea.com) (66.248.192.21) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 05 Mar 2007 08:28:34 -0800 Received: from usremr01.bea.com (mailrelay.bea.com [10.160.29.91]) by usremg01.bea.com (Switch-3.2.2/Switch-3.2.2) with ESMTP id l25GSBsN014299 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=FAIL); Mon, 5 Mar 2007 08:28:17 -0800 Received: from repbex02.amer.bea.com (repbex02.bea.com [10.160.26.99]) by usremr01.bea.com (Switch-3.2.2/Switch-3.2.2) with ESMTP id l25GS5K4008456; Mon, 5 Mar 2007 08:28:05 -0800 X-MimeOLE: Produced By Microsoft Exchange V6.5 Content-class: urn:content-classes:message MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Subject: RE: Entity manager injection Date: Mon, 5 Mar 2007 08:28:07 -0800 Message-ID: <7D856CDFE035FF45A0420ACBD71BDD630366E892@repbex02.amer.bea.com> In-Reply-To: X-MS-Has-Attach: X-MS-TNEF-Correlator: Thread-Topic: Entity manager injection Thread-Index: Acdc6HHgsYiSh4KFThyT27tDsFyXlwCWJdlA References: From: "Patrick Linskey" To: , X-PMX-Version: 4.7.1.128075, Antispam-Engine: 2.5.0.283055, Antispam-Data: 2007.2.21.161433 X-Virus-Checked: Checked by ClamAV on apache.org 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 --=20 Patrick Linskey BEA Systems, Inc.=20 _______________________________________________________________________ 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.=20 > -----Original Message----- > From: Matthieu Riou [mailto:matthieu.riou@gmail.com]=20 > Sent: Friday, March 02, 2007 8:32 AM > To: open-jpa-dev@incubator.apache.org > Subject: Entity manager injection >=20 > Hi, >=20 > For ODE we're managing our transactions ourselves. We start=20 > them and commit > them explicitly. By the same token we create the EntityManagerFactory > ourselves. Is there a possibility to use the EntityManager=20 > 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=20 > persistent classes > using it? >=20 > I've tried doing something like: >=20 > 1. Adding "@PersistenceContext private EntityManager _em;"=20 > in a persistence > class. > 2. Load the persistence class from an EntityManager created with the > factory. > 3. Use _em in my persistent class > =3D> NullPointerException >=20 > Thanks, > Matthieu >=20