You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@openjpa.apache.org by Majeed Arni <ma...@sbcglobal.net> on 2007/05/10 19:16:00 UTC

Accessing Trasaction or EnityManager in LifeCycle methods



  If I have @PrePersist or @PreRemove, in the methods that gets called, how
can I access the Transaction object?

Example:

	@PrePersist
	public void prePersist()
	{
             em.persist(anotherObj);   // How do I get access to em?
             ....
         }

Thanks for the help.
-- 
View this message in context: http://www.nabble.com/Accessing-Trasaction-or-EnityManager-in-LifeCycle-methods-tf3722974.html#a10417736
Sent from the open-jpa-dev mailing list archive at Nabble.com.


Re: Accessing Trasaction or EnityManager in LifeCycle methods

Posted by Patrick Linskey <pl...@gmail.com>.
There is no JPA spec-mandated way to do this. In OpenJPA, you can do
the following:

EntityManager em = OpenJPAPersistence.getEntityManager(this);

Note that the JPA spec does not require that impls allow you to
operate on the EM during a callback. OpenJPA does allow this behavior.

You might also be interested in the OpenJPA transaction-level or
lifecycle-level callbacks. See the listener interfaces in
org.apache.openjpa.event. You can add these with a call to
OpenJPAEntityManager.addTransactionListener() and
OpenJPAEntityManager.addLifecycleListener().

-Patrick

On 5/10/07, Majeed Arni <ma...@sbcglobal.net> wrote:
>
>
>
>   If I have @PrePersist or @PreRemove, in the methods that gets called, how
> can I access the Transaction object?
>
> Example:
>
>         @PrePersist
>         public void prePersist()
>         {
>              em.persist(anotherObj);   // How do I get access to em?
>              ....
>          }
>
> Thanks for the help.
> --
> View this message in context: http://www.nabble.com/Accessing-Trasaction-or-EnityManager-in-LifeCycle-methods-tf3722974.html#a10417736
> Sent from the open-jpa-dev mailing list archive at Nabble.com.
>
>


-- 
Patrick Linskey
202 669 5907