openjpa-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Craig L Russell <Craig.Russ...@Sun.COM>
Subject Re: Entity manager injection
Date Mon, 05 Mar 2007 19:19:21 GMT
Hi Matthieu,

On Mar 5, 2007, at 10:09 AM, Matthieu Riou wrote:

> Hi Craig,
>
> I plainly understand the rational when your application is living  
> in an EJB
> container and relies on session beans. However when you're in a  
> standalone
> environment (like Apache Ode is) or only with a servlet container,  
> there are
> many cases where you'll want your Entity to know about the EM. It  
> allows you
> to have a nice design with some entities being responsible of the  
> lifecycle
> of other entities they manage.

Ah, now I understand. What you want is a method that allows you to  
get the EntityManager for "this" Entity.

<gratuitous-gripe>
Sadly, the 220 expert group thought that this was not a good enough  
design pattern to be included in the specification. If you were using  
JDO, you could simply use the standard static method  
JDOHelper.getPersistenceManager(this).
</gratuitous-gripe>

In OpenJPA, you can use the static method  
org.apache.openjpa.persistence.OpenJPAPersistence.getEntityManager 
(this) to get a reference to the managing EntityManager if there is  
one. There will not be one for a detached instance or a new instance.

This will work dynamically. If you want to store the EntityManager in  
a field, you would use a callback to set the value at the life cycle  
stages where the EntityManager is assigned (or cleared). That would  
be PostPersist and PostLoad IIRC. But there's no callback when  
detaching an instance, so you would have to check when using it to  
make sure it was valid, as in
boolean checkEMValid() {
   if (em == null)
     return false;
   if (em.contains(this))
     return true;
   em=null; //must have been detached
   return false;
}

Craig
>
> For example in Ode the process entity creates and finds process  
> instances,
> all the persistence details being hidden behind generic (as non  
> persistence
> specific) interfaces.
>
> Thanks a lot for the reply and the clarification,
> Matthieu
>
> On 3/5/07, Craig L Russell <Craig.Russell@sun.com> wrote:
>>
>> Hi Matthew,
>>
>> Basically, with JPA we have formalized separation of concerns between
>> the user's Domain Object Model, represented by Entity, and the user's
>> Business Object, represented by Session Beans.
>>
>> There is no need for managing an EntityManager from the Entity
>> itself. So you should simply remove references to the em in your
>> Entity classes, and put them instead into your Session Beans.
>>
>> So injection doesn't work at all for Entity, but works for Session
>> Beans.
>>
>> Craig
>>
>> On Mar 5, 2007, at 9:28 AM, Matthieu Riou wrote:
>>
>> >> 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.
>> >
>> >
>> > I see, so if what JPA needs is just a JTA transaction then yes
>> > we're in that
>> > category. So I guess the next question is do you have an idea why
>> > my entity
>> > manager is null when I use:
>> >
>> > @Entity
>> > public class Foo {
>> >   @PersistenceContext private EntityManager em;
>> >   ...
>> > }
>> >
>> > ?? These 3 links point to the place we initialize the EM factory,
>> > the EM and
>> > a the persistence object that uses the EM and would like to use
>> > injection:
>> >
>> > http://svn.apache.org/repos/asf/incubator/ode/trunk/dao-jpa/src/
>> > main/java/org/apache/ode/dao/jpa/BPELDAOConnectionFactoryImpl.java
>> > http://svn.apache.org/repos/asf/incubator/ode/trunk/dao-jpa/src/
>> > main/java/org/apache/ode/dao/jpa/BPELDAOConnectionImpl.java
>> > http://svn.apache.org/repos/asf/incubator/ode/trunk/dao-jpa/src/
>> > main/java/org/apache/ode/dao/jpa/ProcessDAOImpl.java
>> >
>> > 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.
>> >
>> >
>> > Makes sense.
>> >
>> > Thanks!
>> > Matthieu
>> >
>> >
>> >
>> >> -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
>> >> >       >
>> >> >
>> >> >
>> >> >
>> >> >
>> >>
>>
>> Craig Russell
>> Architect, Sun Java Enterprise System http://java.sun.com/products/ 
>> jdo
>> 408 276-5638 mailto:Craig.Russell@sun.com
>> P.S. A good JDO? O, Gasp!
>>
>>
>>

Craig Russell
Architect, Sun Java Enterprise System http://java.sun.com/products/jdo
408 276-5638 mailto:Craig.Russell@sun.com
P.S. A good JDO? O, Gasp!


Mime
View raw message