You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Miroslav Nachev <mi...@space-comm.com> on 2008/01/17 22:18:31 UTC

How to get EntityManager in callback methods (Pre/Post Remove, Update, Persist)

Hi,

In callback methods (PreRemove, PreUpdate, PrePersist, PostRemove,
PostUpdate, PostPersist) of an Entity Bean I need to execute native SQL
statement in the same transaction. How to retrieve current EntityManager
from which to get Connection object?
Are there any helper tools in OpenJPA for that purpose?


Regards,
Miro.

Re: How to get EntityManager in callback methods (Pre/Post Remove, Update, Persist)

Posted by Patrick Linskey <pl...@gmail.com>.
Hi,

You can call OpenJPAPersistence.getEntityManager(this) to get the EntityManager.

-Patrick

On Jan 20, 2008 7:47 AM, Miroslav Nachev <mi...@space-comm.com> wrote:
> Hi Prashant,
>
> Thanks for the ideas.
> Unfortunately I am using GlassFish v2 (EJB 3) but I think that you give some
> ideas to try. If this is not working I will ask again the community.
> Also I have one idea to hacl OpenJPA as follow:
> 1. Check if the callback method StoreContext as parameter;
> 2. If the 1st point is true, then pass StoreContext to that callback method.
>
>
> Miro.
>
>
> On 1/20/08, Prashant Bhat <pr...@gmail.com> wrote:
> >
> > Hi,
> >
> > How are you adding the entity listeners(directly to the entities or to
> > entity manager factory)? If you add it for the entity, I doubt, it's
> > possible to get the reference to current em.
> >
> > As I use Spring framework, it's quite easy:-) The
> > MyEntityLifecycleEventHandler adds itself as a listener to emf and listens
> > to all changes.
> > ((OpenJPAEntityManagerFactorySPI)
> > emf).addLifecycleListener(entityLifecycleEventHandler , (Class[]) null);
> >
> > Also this is a Spring managed bean in my case, so then one can use
> > @PersistenceContext to inject the current entity manager. Although, I've
> > not
> > used it like this, as I didn't have such a requirement, I believe, it
> > should
> > work.
> >
> > If you're not using spring, then I don't know other ways. sorry!
> > (Probably,
> > you should manage setting the current em to entityLifecycleEventHandler
> > every time!)
> >
> > HTH,
> > Prashant
> >
> >
> > On Jan 20, 2008 6:33 PM, Miroslav Nachev <mi...@space-comm.com> wrote:
> >
> > >  Hi,
> > >
> > > I am writing this message again because until now I haven't answer and
> > in
> > > the same time this is very important for my application.
> > > Can somebody help me?
> > >
> > > Regards,
> > > Miro.
> > >
> > >
> > >
> > > Miroslav Nachev wrote:
> > >
> > > In JDBCSeq, StoreContext (JDBCStore can be retrieved) is passed as
> > > parameter. If this can be passed as parameter in callback methods or if
> > it
> > > can be retrieved from some OpenJPA Utility will be very good.
> > > Do you have any ideas or suggestion? Can you help me?
> > >
> > >
> > > Miro.
> > >
> > > On 1/17/08, Miroslav Nachev <mi...@space-comm.com> wrote:
> > > >
> > > > Hi,
> > > >
> > > > In callback methods (PreRemove, PreUpdate, PrePersist, PostRemove,
> > > > PostUpdate, PostPersist) of an Entity Bean I need to execute native
> > SQL
> > > > statement in the same transaction. How to retrieve current
> > EntityManager
> > > > from which to get Connection object?
> > > > Are there any helper tools in OpenJPA for that purpose?
> > > >
> > > >
> > > > Regards,
> > > > Miro.
> > > >
> > >
> > >
> > >
> >
>



-- 
Patrick Linskey
202 669 5907

Re: How to get EntityManager in callback methods (Pre/Post Remove, Update, Persist)

Posted by Miroslav Nachev <mi...@space-comm.com>.
Hi Prashant,

Thanks for the ideas.
Unfortunately I am using GlassFish v2 (EJB 3) but I think that you give some
ideas to try. If this is not working I will ask again the community.
Also I have one idea to hacl OpenJPA as follow:
1. Check if the callback method StoreContext as parameter;
2. If the 1st point is true, then pass StoreContext to that callback method.


Miro.

On 1/20/08, Prashant Bhat <pr...@gmail.com> wrote:
>
> Hi,
>
> How are you adding the entity listeners(directly to the entities or to
> entity manager factory)? If you add it for the entity, I doubt, it's
> possible to get the reference to current em.
>
> As I use Spring framework, it's quite easy:-) The
> MyEntityLifecycleEventHandler adds itself as a listener to emf and listens
> to all changes.
> ((OpenJPAEntityManagerFactorySPI)
> emf).addLifecycleListener(entityLifecycleEventHandler , (Class[]) null);
>
> Also this is a Spring managed bean in my case, so then one can use
> @PersistenceContext to inject the current entity manager. Although, I've
> not
> used it like this, as I didn't have such a requirement, I believe, it
> should
> work.
>
> If you're not using spring, then I don't know other ways. sorry!
> (Probably,
> you should manage setting the current em to entityLifecycleEventHandler
> every time!)
>
> HTH,
> Prashant
>
>
> On Jan 20, 2008 6:33 PM, Miroslav Nachev <mi...@space-comm.com> wrote:
>
> >  Hi,
> >
> > I am writing this message again because until now I haven't answer and
> in
> > the same time this is very important for my application.
> > Can somebody help me?
> >
> > Regards,
> > Miro.
> >
> >
> >
> > Miroslav Nachev wrote:
> >
> > In JDBCSeq, StoreContext (JDBCStore can be retrieved) is passed as
> > parameter. If this can be passed as parameter in callback methods or if
> it
> > can be retrieved from some OpenJPA Utility will be very good.
> > Do you have any ideas or suggestion? Can you help me?
> >
> >
> > Miro.
> >
> > On 1/17/08, Miroslav Nachev <mi...@space-comm.com> wrote:
> > >
> > > Hi,
> > >
> > > In callback methods (PreRemove, PreUpdate, PrePersist, PostRemove,
> > > PostUpdate, PostPersist) of an Entity Bean I need to execute native
> SQL
> > > statement in the same transaction. How to retrieve current
> EntityManager
> > > from which to get Connection object?
> > > Are there any helper tools in OpenJPA for that purpose?
> > >
> > >
> > > Regards,
> > > Miro.
> > >
> >
> >
> >
>

Re: How to get EntityManager in callback methods (Pre/Post Remove, Update, Persist)

Posted by Prashant Bhat <pr...@gmail.com>.
Hi,

How are you adding the entity listeners(directly to the entities or to
entity manager factory)? If you add it for the entity, I doubt, it's
possible to get the reference to current em.

As I use Spring framework, it's quite easy:-) The
MyEntityLifecycleEventHandler adds itself as a listener to emf and listens
to all changes.
((OpenJPAEntityManagerFactorySPI)
emf).addLifecycleListener(entityLifecycleEventHandler , (Class[]) null);

Also this is a Spring managed bean in my case, so then one can use
@PersistenceContext to inject the current entity manager. Although, I've not
used it like this, as I didn't have such a requirement, I believe, it should
work.

If you're not using spring, then I don't know other ways. sorry! (Probably,
you should manage setting the current em to entityLifecycleEventHandler
every time!)

HTH,
Prashant


On Jan 20, 2008 6:33 PM, Miroslav Nachev <mi...@space-comm.com> wrote:

>  Hi,
>
> I am writing this message again because until now I haven't answer and in
> the same time this is very important for my application.
> Can somebody help me?
>
> Regards,
> Miro.
>
>
>
> Miroslav Nachev wrote:
>
> In JDBCSeq, StoreContext (JDBCStore can be retrieved) is passed as
> parameter. If this can be passed as parameter in callback methods or if it
> can be retrieved from some OpenJPA Utility will be very good.
> Do you have any ideas or suggestion? Can you help me?
>
>
> Miro.
>
> On 1/17/08, Miroslav Nachev <mi...@space-comm.com> wrote:
> >
> > Hi,
> >
> > In callback methods (PreRemove, PreUpdate, PrePersist, PostRemove,
> > PostUpdate, PostPersist) of an Entity Bean I need to execute native SQL
> > statement in the same transaction. How to retrieve current EntityManager
> > from which to get Connection object?
> > Are there any helper tools in OpenJPA for that purpose?
> >
> >
> > Regards,
> > Miro.
> >
>
>
>

Re: How to get EntityManager in callback methods (Pre/Post Remove, Update, Persist)

Posted by Miroslav Nachev <mi...@space-comm.com>.
In JDBCSeq, StoreContext (JDBCStore can be retrieved) is passed as
parameter. If this can be passed as parameter in callback methods or if it
can be retrieved from some OpenJPA Utility will be very good.
Do you have any ideas or suggestion? Can you help me?


Miro.

On 1/17/08, Miroslav Nachev <mi...@space-comm.com> wrote:
>
> Hi,
>
> In callback methods (PreRemove, PreUpdate, PrePersist, PostRemove,
> PostUpdate, PostPersist) of an Entity Bean I need to execute native SQL
> statement in the same transaction. How to retrieve current EntityManager
> from which to get Connection object?
> Are there any helper tools in OpenJPA for that purpose?
>
>
> Regards,
> Miro.
>