openjpa-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Patrick Linskey" <plins...@bea.com>
Subject RE: Entity manager injection
Date Mon, 05 Mar 2007 17:04:40 GMT
> We're managing our own transactions using our 
> TransactionManager provided through "openjpa.ManagedRuntime". 
> So I guess we'll have to go for your second solution. I'll 
> probably implement a simple ThreadLocal holding the 
> EntityManager associated with the current tx. 

Hmm. I believe that you actually fall into the first category -- OpenJPA
only uses the ManagedRuntime when it thinks that it's integrating with
JTA. It sounds like you're using JTA to control transactions, and
plugging into OpenJPA SPIs to tell OpenJPA about how to bind into JTA.

> Btw it's just an idea but wouldn't it be possible for OpenJPA 
> to handle persistence manager injection even outside of an 
> EJB container? It seems to me that all the necessary hooks 
> exist. Just an idea though :) 

The hooks certainly do exist, for the most part. However, we already
have an API-based way to do this --
OpenJPAPersistence.getEntityManager(Object). Since injecting an
instance's EntityManager not quite the same as what happens with the
@PersistenceContext annotation from a Java EE standpoint, I prefer to
recommend the proprietary OpenJPA extension API. This makes it more
clear where you are depending on OpenJPA-specific features, and avoids
any semantic confusion.

-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:matthieu.riou@gmail.com] 
> Sent: Monday, March 05, 2007 8:48 AM
> To: Patrick Linskey
> Cc: open-jpa-dev@incubator.apache.org
> Subject: Re: Entity manager injection
> 
> We're managing our own transactions using our 
> TransactionManager provided through "openjpa.ManagedRuntime". 
> So I guess we'll have to go for your second solution. I'll 
> probably implement a simple ThreadLocal holding the 
> EntityManager associated with the current tx. 
> 
> Btw it's just an idea but wouldn't it be possible for OpenJPA 
> to handle persistence manager injection even outside of an 
> EJB container? It seems to me that all the necessary hooks 
> exist. Just an idea though :) 
> 
> Thanks for the help,
> Matthieu
> 
> 
> On 3/5/07, Patrick Linskey <plinskey@bea.com> wrote:
> 
> 	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: matthieu.riou@gmail.com 
> <mailto:matthieu.riou@gmail.com> ]
> 	> 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
> 	>
> 	
> 
> 
> 

Mime
View raw message