You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by illhan <qu...@yahoo.com.cn> on 2009/08/06 10:44:40 UTC

how to use openjpa event listener ?

In the javadoc there are many event and listener.I want to use the event and
the listener in jee enviroment,the emf.add**Listener() method can't not
called because in jee enviroment the entitymanagerfacetory is created by jee
contianer.i only use ejb3 injection in my session bean use  
@PersistenceContext
    private EntityManager em;
so i don't know how to register the Listener(such as LifecycleListener) in
JEE enviroment.

illhan
-- 
View this message in context: http://n2.nabble.com/how-to-use-openjpa-event-listener---tp3397042p3397042.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: how to use openjpa event listener ?

Posted by illhan <qu...@yahoo.com.cn>.
hi Kevin,
I know i can cast the  EntityManager to OpenJPAEntityManagerSPI.In jee
environment,the EntityManager usally be injected in session bean(ejb3).If i
cast the EntityManager to OpenJPAEntityManagerSPI in session beans,should I
register the cast the type in my all sesson beans?
In my opinion I can regiester LifeCycleListener in the application init
phase,not in java code.I wish to use the LifeCycleListener in the config
files.
Thanks Kevin

illhan


Kevin Sutter wrote:
> 
> I agree with Mike that the methods you want to use are part of the OpenJPA
> SPI layer and, thus, are subject to change.  It would be good to
> understand
> your interest in these SPI's.  But, ...
> 
> Even in a container-managed environment, you have access to the
> EntityManager.  You could cast the EntityManager to
> OpenJPAEntityManagerSPI
> and have access to these methods.
> 
> Kevin
> 
> On Tue, Aug 11, 2009 at 8:51 AM, Michael Dick
> <mi...@gmail.com>wrote:
> 
>> Hi Illhan,
>>
>> The method you want to use is part of OpenJPA's SPI layer, and isn't
>> intended to be used by most applications. As a result the interface may
>> be
>> a
>> bit more fluid than our 'normal' API methods.
>>
>> Could you elaborate on why you want to add a LifeCycleListener? We might
>> be
>> able to suggest an alternative that accomplishes the same goal..
>>
>> Best Regards,
>> -mike
>>
>> On Mon, Aug 10, 2009 at 9:05 PM, illhan <qu...@yahoo.com.cn> wrote:
>>
>> >
>> > hi Rick,
>> > You means the JPA lifecycle Callbacks,But I want to know the openjpa's
>> > listener,in package org.apache.openjpa.event,the interface
>> > LifecycleListener
>> > and the class LifecycleEvents.
>> > From the javadoc,the LifecycleListener  can be registered by the method
>> > addLifecycleListener(Object listener, Class... classes) from class
>> > org.apache.openjpa.persistence.EntityManagerImpl,but in JEE container
>> the
>> > EntityManager is inited by the container itself.How can I registere the
>> > LifecycleListener and process the LifecycleEvents.I have reviewed the
>> > openjpa user's guide,but no part can be found there.
>> > Thanks Rick.
>> > illhan
>> >
>> > Rick Curtis wrote:
>> > >
>> > > Illhan -
>> > >
>> > > Did you read the section in the user manual detailing the usage of
>> > > Lifecycle Callbacks [1]?
>> > >
>> > > -Rick
>> > >
>> > > [1]
>> >
>> http://openjpa.apache.org/builds/latest/docs/manual/manual.html#jpa_overview_pc_callbacks
>> > >
>> > >
>> > > illhan wrote:
>> > >>
>> > >> hi Rick,
>> > >> The EntityManager and the EntityManagerFactory are initializated by
>> the
>> > >> JEE contianer.I think the LifecycleListener could be set in
>> > >> persistent.xml,not only used by java code.
>> > >> Thanks Rick.
>> > >>
>> > >> illhan
>> > >>
>> > >> Rick Curtis wrote:
>> > >>>
>> > >>> Illhan -
>> > >>> In your application initialization I believe you could do something
>> > like
>> > >>> this to get a reference to the EntityManagerFactory....
>> > >>>
>> > >>>             OpenJPAEntityManager oem = OpenJPAPersistence.cast(em);
>> > >>>             OpenJPAEntityManagerFactory oemf =
>> > >>> OpenJPAPersistence.cast(oem.getEntityManagerFactory());
>> > >>>
>> > >>> - Rick
>> > >>>
>> > >>> illhan wrote:
>> > >>>>
>> > >>>> In the javadoc there are many event and listener.I want to use the
>> > >>>> event and the listener in jee enviroment,the emf.add**Listener()
>> > method
>> > >>>> can't not called because in jee enviroment the
>> entitymanagerfacetory
>> > is
>> > >>>> created by jee contianer.i only use ejb3 injection in my session
>> bean
>> > >>>> use   @PersistenceContext
>> > >>>>     private EntityManager em;
>> > >>>> so i don't know how to register the Listener(such as
>> > LifecycleListener)
>> > >>>> in JEE enviroment.
>> > >>>>
>> > >>>> illhan
>> > >>>>
>> > >>>
>> > >>>
>> > >>
>> > >>
>> > >
>> > >
>> >
>> > --
>> > View this message in context:
>> >
>> http://n2.nabble.com/how-to-use-openjpa-event-listener---tp3397042p3421303.html
>> > Sent from the OpenJPA Users mailing list archive at Nabble.com.
>> >
>>
> 
> 

-- 
View this message in context: http://n2.nabble.com/how-to-use-openjpa-event-listener---tp3397042p3424586.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: how to use openjpa event listener ?

Posted by Kevin Sutter <kw...@gmail.com>.
I agree with Mike that the methods you want to use are part of the OpenJPA
SPI layer and, thus, are subject to change.  It would be good to understand
your interest in these SPI's.  But, ...

Even in a container-managed environment, you have access to the
EntityManager.  You could cast the EntityManager to OpenJPAEntityManagerSPI
and have access to these methods.

Kevin

On Tue, Aug 11, 2009 at 8:51 AM, Michael Dick <mi...@gmail.com>wrote:

> Hi Illhan,
>
> The method you want to use is part of OpenJPA's SPI layer, and isn't
> intended to be used by most applications. As a result the interface may be
> a
> bit more fluid than our 'normal' API methods.
>
> Could you elaborate on why you want to add a LifeCycleListener? We might be
> able to suggest an alternative that accomplishes the same goal..
>
> Best Regards,
> -mike
>
> On Mon, Aug 10, 2009 at 9:05 PM, illhan <qu...@yahoo.com.cn> wrote:
>
> >
> > hi Rick,
> > You means the JPA lifecycle Callbacks,But I want to know the openjpa's
> > listener,in package org.apache.openjpa.event,the interface
> > LifecycleListener
> > and the class LifecycleEvents.
> > From the javadoc,the LifecycleListener  can be registered by the method
> > addLifecycleListener(Object listener, Class... classes) from class
> > org.apache.openjpa.persistence.EntityManagerImpl,but in JEE container the
> > EntityManager is inited by the container itself.How can I registere the
> > LifecycleListener and process the LifecycleEvents.I have reviewed the
> > openjpa user's guide,but no part can be found there.
> > Thanks Rick.
> > illhan
> >
> > Rick Curtis wrote:
> > >
> > > Illhan -
> > >
> > > Did you read the section in the user manual detailing the usage of
> > > Lifecycle Callbacks [1]?
> > >
> > > -Rick
> > >
> > > [1]
> >
> http://openjpa.apache.org/builds/latest/docs/manual/manual.html#jpa_overview_pc_callbacks
> > >
> > >
> > > illhan wrote:
> > >>
> > >> hi Rick,
> > >> The EntityManager and the EntityManagerFactory are initializated by
> the
> > >> JEE contianer.I think the LifecycleListener could be set in
> > >> persistent.xml,not only used by java code.
> > >> Thanks Rick.
> > >>
> > >> illhan
> > >>
> > >> Rick Curtis wrote:
> > >>>
> > >>> Illhan -
> > >>> In your application initialization I believe you could do something
> > like
> > >>> this to get a reference to the EntityManagerFactory....
> > >>>
> > >>>             OpenJPAEntityManager oem = OpenJPAPersistence.cast(em);
> > >>>             OpenJPAEntityManagerFactory oemf =
> > >>> OpenJPAPersistence.cast(oem.getEntityManagerFactory());
> > >>>
> > >>> - Rick
> > >>>
> > >>> illhan wrote:
> > >>>>
> > >>>> In the javadoc there are many event and listener.I want to use the
> > >>>> event and the listener in jee enviroment,the emf.add**Listener()
> > method
> > >>>> can't not called because in jee enviroment the entitymanagerfacetory
> > is
> > >>>> created by jee contianer.i only use ejb3 injection in my session
> bean
> > >>>> use   @PersistenceContext
> > >>>>     private EntityManager em;
> > >>>> so i don't know how to register the Listener(such as
> > LifecycleListener)
> > >>>> in JEE enviroment.
> > >>>>
> > >>>> illhan
> > >>>>
> > >>>
> > >>>
> > >>
> > >>
> > >
> > >
> >
> > --
> > View this message in context:
> >
> http://n2.nabble.com/how-to-use-openjpa-event-listener---tp3397042p3421303.html
> > Sent from the OpenJPA Users mailing list archive at Nabble.com.
> >
>

RE: how to use openjpa event listener ?

Posted by C N Davies <cn...@cndavies.com>.
I'm no expert, but for audit purposes I write my code such that it always
accesses the entity manager via a service class, that way I can log all
persist/merge/delete etc operations and I have added a log viewer to my app
so administrators or finance auditors can check what operations have been
performed.

-----Original Message-----
From: illhan [mailto:quzheng911@yahoo.com.cn] 
Sent: Wednesday, 12 August 2009 1:19 AM
To: users@openjpa.apache.org
Subject: Re: how to use openjpa event listener ?


hi Rick,
Because of the audit logging,I used the openJPA LifeCycleListener(maybe
other openjpa listerners) for audit logging.
I have try to record the audit logging by JPA lifecycle callbacks,but in the
callbacks method's I can not get the old state,I only get the new method.
The document 
http://webspherepersistence.blogspot.com/2009/01/auditing-with-openjpa.html
http://webspherepersistence.blogspot.com/2009/01/auditing-with-openjpa.html 
shows one method to record audit logging,but  the audit method must be added

in my all entites ,it is too complex for developers.
in my opinion,the openJPA LifeCycleListener maybe the best way to reduse the
developers workload,if it can be used here.I can use event descriptor in
eclipselink(toplink),I only to register the event descriptor in
persistence.xml.So I would like to found the similar way in openJPA
environment.

Thanks Rick.
illhan

Michael Dick wrote:
> 
> Hi Illhan,
> The method you want to use is part of OpenJPA's SPI layer, and isn't
> intended to be used by most applications. As a result the interface may be
> a
> bit more fluid than our 'normal' API methods.
> 
> Could you elaborate on why you want to add a LifeCycleListener? We might
> be
> able to suggest an alternative that accomplishes the same goal..
> 
> Best Regards,
> -mike
> 
> On Mon, Aug 10, 2009 at 9:05 PM, illhan <qu...@yahoo.com.cn> wrote:
> 
>>
>> hi Rick,
>> You means the JPA lifecycle Callbacks,But I want to know the openjpa's
>> listener,in package org.apache.openjpa.event,the interface
>> LifecycleListener
>> and the class LifecycleEvents.
>> From the javadoc,the LifecycleListener  can be registered by the method
>> addLifecycleListener(Object listener, Class... classes) from class
>> org.apache.openjpa.persistence.EntityManagerImpl,but in JEE container the
>> EntityManager is inited by the container itself.How can I registere the
>> LifecycleListener and process the LifecycleEvents.I have reviewed the
>> openjpa user's guide,but no part can be found there.
>> Thanks Rick.
>> illhan
>>
>> Rick Curtis wrote:
>> >
>> > Illhan -
>> >
>> > Did you read the section in the user manual detailing the usage of
>> > Lifecycle Callbacks [1]?
>> >
>> > -Rick
>> >
>> > [1]
>>
http://openjpa.apache.org/builds/latest/docs/manual/manual.html#jpa_overview
_pc_callbacks
>> >
>> >
>> > illhan wrote:
>> >>
>> >> hi Rick,
>> >> The EntityManager and the EntityManagerFactory are initializated by
>> the
>> >> JEE contianer.I think the LifecycleListener could be set in
>> >> persistent.xml,not only used by java code.
>> >> Thanks Rick.
>> >>
>> >> illhan
>> >>
>> >> Rick Curtis wrote:
>> >>>
>> >>> Illhan -
>> >>> In your application initialization I believe you could do something
>> like
>> >>> this to get a reference to the EntityManagerFactory....
>> >>>
>> >>>             OpenJPAEntityManager oem = OpenJPAPersistence.cast(em);
>> >>>             OpenJPAEntityManagerFactory oemf =
>> >>> OpenJPAPersistence.cast(oem.getEntityManagerFactory());
>> >>>
>> >>> - Rick
>> >>>
>> >>> illhan wrote:
>> >>>>
>> >>>> In the javadoc there are many event and listener.I want to use the
>> >>>> event and the listener in jee enviroment,the emf.add**Listener()
>> method
>> >>>> can't not called because in jee enviroment the entitymanagerfacetory
>> is
>> >>>> created by jee contianer.i only use ejb3 injection in my session
>> bean
>> >>>> use   @PersistenceContext
>> >>>>     private EntityManager em;
>> >>>> so i don't know how to register the Listener(such as
>> LifecycleListener)
>> >>>> in JEE enviroment.
>> >>>>
>> >>>> illhan
>> >>>>
>> >>>
>> >>>
>> >>
>> >>
>> >
>> >
>>
>> --
>> View this message in context:
>>
http://n2.nabble.com/how-to-use-openjpa-event-listener---tp3397042p3421303.h
tml
>> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>>
> 
> 

-- 
View this message in context:
http://n2.nabble.com/how-to-use-openjpa-event-listener---tp3397042p3424516.h
tml
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Re: how to use openjpa event listener ?

Posted by Pinaki Poddar <pp...@apache.org>.
> I used runitme enhanced

> I follow the steps by example descripted in the
> http://webspherepersistence.blogspot.com/2009/01/auditing-with-openjpa.htm

Not fully :) Note the following at the very end of the article  
The two critical points before we close.
  * This technique only works with build-time enhancement.


illhan wrote:
> 
> hi Michael,
> I follow the steps by example descripted in the 
> http://webspherepersistence.blogspot.com/2009/01/auditing-with-openjpa.htm
> http://webspherepersistence.blogspot.com/2009/01/auditing-with-openjpa.htm
> .
> But when I update the entity,the callback calls @PostUpdate method.In the
> callback method, I got the old state of the entity,but the unchanged the
> fields value are all "null",only the changed fileds holds the old state
> value.I used runitme enhanced and config the property
> openjpa.RestoreState= all.I can not get the reason fot it?Anything other
> config propertie needer here?
> Thanks Michael.
> 
> illhan
> 
> 
> Michael Dick wrote:
>> 
>> Hi Ilhan,
>> 
>> Maybe I don't understand the request. Pinaki's
>> example<http://webspherepersistence.blogspot.com/2009/01/auditing-with-openjpa.html>uses
>> the JPA spec defined callbacks to get the previous state of the
>> entity.
>> I thought the problem you were hitting was the need to put the audit
>> method
>> in each entity. Using orm.xml you can define a single PostUpdate method
>> which applies to every entity (of course that audit method may be tightly
>> coupled to the entities in your PersistenceUnit).
>> 
>> Based on Pinaki's example I think this would work for you.
>> 
>> On to your second question :
>> I don't think you can inject a PersistenceContext or an EJB into a
>> lifecycle
>> callback method and you'd want to use a jndi lookup in this case. I'm not
>> an
>> expert with injection though so I could be proven wrong.
>> 
>> Best Regards,
>> 
>> -mike
>> 
>> On Wed, Aug 12, 2009 at 3:42 AM, illhan <qu...@yahoo.com.cn> wrote:
>> 
>>>
>>> hi Michael,
>>> I know the entity lifecyclelistener config in orm.xm,Thanks.
>>> I think the entity lifecyclelistener is not fit for audit logging.I can
>>> not
>>> get old state from the lifecyclelistener,and the lifecyclelistener is
>>> JPA
>>> spec,not JEE5,so session bean or persistencecontext can not be injected
>>> in
>>> lifecyclelistener,I must use the jndi lookup to find ejb or
>>> entitymanager,is
>>> it right?
>>> Thanks Michael.
>>> illhan
>>>
>>> Michael Dick wrote:
>>> >
>>> > Hi Illhan,
>>> >
>>> > The audit example from the persistence blog uses a @PostUpdate
>>> listener
>>> to
>>> > do the auditing. You can specify a default set of listeners for an
>>> entire
>>> > persistence unit in an xml mapping file. For example :
>>> >
>>> > persistence.xml :
>>> >
>>> > <persistence-unit name="auditable-pu">
>>> >      <mapping-file>orm.xml</mapping-file>
>>> > </persistence-unit>
>>> >
>>> > orm/xml :
>>> > <persistence-unit-metadata>
>>> >    <persistence-unit-defaults>
>>> >        <entity-listeners>
>>> >             <entity-listener class="foo.bar.Auditor">
>>> >                  <pre-persist method-name="prePersist" />
>>> >                  <post-persist method-name="postPersist" />
>>> >                  <pre-remove method-name="preRemove" />
>>> >                  <post-remove method-name="postRemove" />
>>> >                  <pre-update method-name="preUpdate" />
>>> >                  <post-update method-name="postUpdate" />
>>> >                  <post-load method-name="postLoad" />
>>> >             </entity-listener>
>>> >         </entity-listeners>
>>> >    </persistence-unit-defaults>
>>> > </persistence-unit-metadata>
>>> >
>>> >
>>> > Foo.bar.Auditor.postUpdate() will operate on all the entities in your
>>> > persistence unit - just like the annotation example in Pinaki's blog
>>> post.
>>> >
>>> > Does that sound like what you want?
>>> >
>>> > -mike
>>> >
>>> > On Tue, Aug 11, 2009 at 10:19 AM, illhan <qu...@yahoo.com.cn>
>>> wrote:
>>> >
>>> >>
>>> >> hi Rick,
>>> >> Because of the audit logging,I used the openJPA
>>> LifeCycleListener(maybe
>>> >> other openjpa listerners) for audit logging.
>>> >> I have try to record the audit logging by JPA lifecycle callbacks,but
>>> in
>>> >> the
>>> >> callbacks method's I can not get the old state,I only get the new
>>> method.
>>> >> The document
>>> >>
>>> http://webspherepersistence.blogspot.com/2009/01/auditing-with-openjpa.html
>>> >>
>>> http://webspherepersistence.blogspot.com/2009/01/auditing-with-openjpa.html
>>> >> shows one method to record audit logging,but  the audit method must
>>> be
>>> >> added
>>> >> in my all entites ,it is too complex for developers.
>>> >> in my opinion,the openJPA LifeCycleListener maybe the best way to
>>> reduse
>>> >> the
>>> >> developers workload,if it can be used here.I can use event descriptor
>>> in
>>> >> eclipselink(toplink),I only to register the event descriptor in
>>> >> persistence.xml.So I would like to found the similar way in openJPA
>>> >> environment.
>>> >>
>>> >> Thanks Rick.
>>> >> illhan
>>> >>
>>> >> Michael Dick wrote:
>>> >> >
>>> >> > Hi Illhan,
>>> >> > The method you want to use is part of OpenJPA's SPI layer, and
>>> isn't
>>> >> > intended to be used by most applications. As a result the interface
>>> may
>>> >> be
>>> >> > a
>>> >> > bit more fluid than our 'normal' API methods.
>>> >> >
>>> >> > Could you elaborate on why you want to add a LifeCycleListener? We
>>> >> might
>>> >> > be
>>> >> > able to suggest an alternative that accomplishes the same goal..
>>> >> >
>>> >> > Best Regards,
>>> >> > -mike
>>> >> >
>>> >> > On Mon, Aug 10, 2009 at 9:05 PM, illhan <qu...@yahoo.com.cn>
>>> >> wrote:
>>> >> >
>>> >> >>
>>> >> >> hi Rick,
>>> >> >> You means the JPA lifecycle Callbacks,But I want to know the
>>> openjpa's
>>> >> >> listener,in package org.apache.openjpa.event,the interface
>>> >> >> LifecycleListener
>>> >> >> and the class LifecycleEvents.
>>> >> >> From the javadoc,the LifecycleListener  can be registered by the
>>> >> method
>>> >> >> addLifecycleListener(Object listener, Class... classes) from class
>>> >> >> org.apache.openjpa.persistence.EntityManagerImpl,but in JEE
>>> container
>>> >> the
>>> >> >> EntityManager is inited by the container itself.How can I
>>> registere
>>> >> the
>>> >> >> LifecycleListener and process the LifecycleEvents.I have reviewed
>>> the
>>> >> >> openjpa user's guide,but no part can be found there.
>>> >> >> Thanks Rick.
>>> >> >> illhan
>>> >> >>
>>> >> >> Rick Curtis wrote:
>>> >> >> >
>>> >> >> > Illhan -
>>> >> >> >
>>> >> >> > Did you read the section in the user manual detailing the usage
>>> of
>>> >> >> > Lifecycle Callbacks [1]?
>>> >> >> >
>>> >> >> > -Rick
>>> >> >> >
>>> >> >> > [1]
>>> >> >>
>>> >>
>>> http://openjpa.apache.org/builds/latest/docs/manual/manual.html#jpa_overview_pc_callbacks
>>> >> >> >
>>> >> >> >
>>> >> >> > illhan wrote:
>>> >> >> >>
>>> >> >> >> hi Rick,
>>> >> >> >> The EntityManager and the EntityManagerFactory are
>>> initializated
>>> by
>>> >> >> the
>>> >> >> >> JEE contianer.I think the LifecycleListener could be set in
>>> >> >> >> persistent.xml,not only used by java code.
>>> >> >> >> Thanks Rick.
>>> >> >> >>
>>> >> >> >> illhan
>>> >> >> >>
>>> >> >> >> Rick Curtis wrote:
>>> >> >> >>>
>>> >> >> >>> Illhan -
>>> >> >> >>> In your application initialization I believe you could do
>>> >> something
>>> >> >> like
>>> >> >> >>> this to get a reference to the EntityManagerFactory....
>>> >> >> >>>
>>> >> >> >>>             OpenJPAEntityManager oem =
>>> >> OpenJPAPersistence.cast(em);
>>> >> >> >>>             OpenJPAEntityManagerFactory oemf =
>>> >> >> >>> OpenJPAPersistence.cast(oem.getEntityManagerFactory());
>>> >> >> >>>
>>> >> >> >>> - Rick
>>> >> >> >>>
>>> >> >> >>> illhan wrote:
>>> >> >> >>>>
>>> >> >> >>>> In the javadoc there are many event and listener.I want to
>>> use
>>> >> the
>>> >> >> >>>> event and the listener in jee enviroment,the
>>> emf.add**Listener()
>>> >> >> method
>>> >> >> >>>> can't not called because in jee enviroment the
>>> >> entitymanagerfacetory
>>> >> >> is
>>> >> >> >>>> created by jee contianer.i only use ejb3 injection in my
>>> session
>>> >> >> bean
>>> >> >> >>>> use   @PersistenceContext
>>> >> >> >>>>     private EntityManager em;
>>> >> >> >>>> so i don't know how to register the Listener(such as
>>> >> >> LifecycleListener)
>>> >> >> >>>> in JEE enviroment.
>>> >> >> >>>>
>>> >> >> >>>> illhan
>>> >> >> >>>>
>>> >> >> >>>
>>> >> >> >>>
>>> >> >> >>
>>> >> >> >>
>>> >> >> >
>>> >> >> >
>>> >> >>
>>> >> >> --
>>> >> >> View this message in context:
>>> >> >>
>>> >>
>>> http://n2.nabble.com/how-to-use-openjpa-event-listener---tp3397042p3421303.html
>>> >> >> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>>> >> >>
>>> >> >
>>> >> >
>>> >>
>>> >> --
>>> >> View this message in context:
>>> >>
>>> http://n2.nabble.com/how-to-use-openjpa-event-listener---tp3397042p3424516.html
>>> >> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>>> >>
>>> >
>>> >
>>>
>>> --
>>> View this message in context:
>>> http://n2.nabble.com/how-to-use-openjpa-event-listener---tp3397042p3429428.html
>>> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>>>
>> 
>> 
> 
> 


-----
Pinaki 
-- 
View this message in context: http://n2.nabble.com/how-to-use-openjpa-event-listener---tp3397042p3464029.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: how to use openjpa event listener ?

Posted by illhan <qu...@yahoo.com.cn>.
hi Michael,
I follow the steps by example descripted in the 
http://webspherepersistence.blogspot.com/2009/01/auditing-with-openjpa.htm
http://webspherepersistence.blogspot.com/2009/01/auditing-with-openjpa.htm .
But when I update the entity,the callback calls @PostUpdate method.In the
callback method, I got the old state of the entity,but the unchanged the
fields value are all "null",only the changed fileds holds the old state
value.I used runitme enhanced and config the property openjpa.RestoreState=
all.I can not get the reason fot it?Anything other config propertie needer
here?
Thanks Michael.

illhan


Michael Dick wrote:
> 
> Hi Ilhan,
> 
> Maybe I don't understand the request. Pinaki's
> example<http://webspherepersistence.blogspot.com/2009/01/auditing-with-openjpa.html>uses
> the JPA spec defined callbacks to get the previous state of the
> entity.
> I thought the problem you were hitting was the need to put the audit
> method
> in each entity. Using orm.xml you can define a single PostUpdate method
> which applies to every entity (of course that audit method may be tightly
> coupled to the entities in your PersistenceUnit).
> 
> Based on Pinaki's example I think this would work for you.
> 
> On to your second question :
> I don't think you can inject a PersistenceContext or an EJB into a
> lifecycle
> callback method and you'd want to use a jndi lookup in this case. I'm not
> an
> expert with injection though so I could be proven wrong.
> 
> Best Regards,
> 
> -mike
> 
> On Wed, Aug 12, 2009 at 3:42 AM, illhan <qu...@yahoo.com.cn> wrote:
> 
>>
>> hi Michael,
>> I know the entity lifecyclelistener config in orm.xm,Thanks.
>> I think the entity lifecyclelistener is not fit for audit logging.I can
>> not
>> get old state from the lifecyclelistener,and the lifecyclelistener is JPA
>> spec,not JEE5,so session bean or persistencecontext can not be injected
>> in
>> lifecyclelistener,I must use the jndi lookup to find ejb or
>> entitymanager,is
>> it right?
>> Thanks Michael.
>> illhan
>>
>> Michael Dick wrote:
>> >
>> > Hi Illhan,
>> >
>> > The audit example from the persistence blog uses a @PostUpdate listener
>> to
>> > do the auditing. You can specify a default set of listeners for an
>> entire
>> > persistence unit in an xml mapping file. For example :
>> >
>> > persistence.xml :
>> >
>> > <persistence-unit name="auditable-pu">
>> >      <mapping-file>orm.xml</mapping-file>
>> > </persistence-unit>
>> >
>> > orm/xml :
>> > <persistence-unit-metadata>
>> >    <persistence-unit-defaults>
>> >        <entity-listeners>
>> >             <entity-listener class="foo.bar.Auditor">
>> >                  <pre-persist method-name="prePersist" />
>> >                  <post-persist method-name="postPersist" />
>> >                  <pre-remove method-name="preRemove" />
>> >                  <post-remove method-name="postRemove" />
>> >                  <pre-update method-name="preUpdate" />
>> >                  <post-update method-name="postUpdate" />
>> >                  <post-load method-name="postLoad" />
>> >             </entity-listener>
>> >         </entity-listeners>
>> >    </persistence-unit-defaults>
>> > </persistence-unit-metadata>
>> >
>> >
>> > Foo.bar.Auditor.postUpdate() will operate on all the entities in your
>> > persistence unit - just like the annotation example in Pinaki's blog
>> post.
>> >
>> > Does that sound like what you want?
>> >
>> > -mike
>> >
>> > On Tue, Aug 11, 2009 at 10:19 AM, illhan <qu...@yahoo.com.cn>
>> wrote:
>> >
>> >>
>> >> hi Rick,
>> >> Because of the audit logging,I used the openJPA
>> LifeCycleListener(maybe
>> >> other openjpa listerners) for audit logging.
>> >> I have try to record the audit logging by JPA lifecycle callbacks,but
>> in
>> >> the
>> >> callbacks method's I can not get the old state,I only get the new
>> method.
>> >> The document
>> >>
>> http://webspherepersistence.blogspot.com/2009/01/auditing-with-openjpa.html
>> >>
>> http://webspherepersistence.blogspot.com/2009/01/auditing-with-openjpa.html
>> >> shows one method to record audit logging,but  the audit method must be
>> >> added
>> >> in my all entites ,it is too complex for developers.
>> >> in my opinion,the openJPA LifeCycleListener maybe the best way to
>> reduse
>> >> the
>> >> developers workload,if it can be used here.I can use event descriptor
>> in
>> >> eclipselink(toplink),I only to register the event descriptor in
>> >> persistence.xml.So I would like to found the similar way in openJPA
>> >> environment.
>> >>
>> >> Thanks Rick.
>> >> illhan
>> >>
>> >> Michael Dick wrote:
>> >> >
>> >> > Hi Illhan,
>> >> > The method you want to use is part of OpenJPA's SPI layer, and isn't
>> >> > intended to be used by most applications. As a result the interface
>> may
>> >> be
>> >> > a
>> >> > bit more fluid than our 'normal' API methods.
>> >> >
>> >> > Could you elaborate on why you want to add a LifeCycleListener? We
>> >> might
>> >> > be
>> >> > able to suggest an alternative that accomplishes the same goal..
>> >> >
>> >> > Best Regards,
>> >> > -mike
>> >> >
>> >> > On Mon, Aug 10, 2009 at 9:05 PM, illhan <qu...@yahoo.com.cn>
>> >> wrote:
>> >> >
>> >> >>
>> >> >> hi Rick,
>> >> >> You means the JPA lifecycle Callbacks,But I want to know the
>> openjpa's
>> >> >> listener,in package org.apache.openjpa.event,the interface
>> >> >> LifecycleListener
>> >> >> and the class LifecycleEvents.
>> >> >> From the javadoc,the LifecycleListener  can be registered by the
>> >> method
>> >> >> addLifecycleListener(Object listener, Class... classes) from class
>> >> >> org.apache.openjpa.persistence.EntityManagerImpl,but in JEE
>> container
>> >> the
>> >> >> EntityManager is inited by the container itself.How can I registere
>> >> the
>> >> >> LifecycleListener and process the LifecycleEvents.I have reviewed
>> the
>> >> >> openjpa user's guide,but no part can be found there.
>> >> >> Thanks Rick.
>> >> >> illhan
>> >> >>
>> >> >> Rick Curtis wrote:
>> >> >> >
>> >> >> > Illhan -
>> >> >> >
>> >> >> > Did you read the section in the user manual detailing the usage
>> of
>> >> >> > Lifecycle Callbacks [1]?
>> >> >> >
>> >> >> > -Rick
>> >> >> >
>> >> >> > [1]
>> >> >>
>> >>
>> http://openjpa.apache.org/builds/latest/docs/manual/manual.html#jpa_overview_pc_callbacks
>> >> >> >
>> >> >> >
>> >> >> > illhan wrote:
>> >> >> >>
>> >> >> >> hi Rick,
>> >> >> >> The EntityManager and the EntityManagerFactory are initializated
>> by
>> >> >> the
>> >> >> >> JEE contianer.I think the LifecycleListener could be set in
>> >> >> >> persistent.xml,not only used by java code.
>> >> >> >> Thanks Rick.
>> >> >> >>
>> >> >> >> illhan
>> >> >> >>
>> >> >> >> Rick Curtis wrote:
>> >> >> >>>
>> >> >> >>> Illhan -
>> >> >> >>> In your application initialization I believe you could do
>> >> something
>> >> >> like
>> >> >> >>> this to get a reference to the EntityManagerFactory....
>> >> >> >>>
>> >> >> >>>             OpenJPAEntityManager oem =
>> >> OpenJPAPersistence.cast(em);
>> >> >> >>>             OpenJPAEntityManagerFactory oemf =
>> >> >> >>> OpenJPAPersistence.cast(oem.getEntityManagerFactory());
>> >> >> >>>
>> >> >> >>> - Rick
>> >> >> >>>
>> >> >> >>> illhan wrote:
>> >> >> >>>>
>> >> >> >>>> In the javadoc there are many event and listener.I want to use
>> >> the
>> >> >> >>>> event and the listener in jee enviroment,the
>> emf.add**Listener()
>> >> >> method
>> >> >> >>>> can't not called because in jee enviroment the
>> >> entitymanagerfacetory
>> >> >> is
>> >> >> >>>> created by jee contianer.i only use ejb3 injection in my
>> session
>> >> >> bean
>> >> >> >>>> use   @PersistenceContext
>> >> >> >>>>     private EntityManager em;
>> >> >> >>>> so i don't know how to register the Listener(such as
>> >> >> LifecycleListener)
>> >> >> >>>> in JEE enviroment.
>> >> >> >>>>
>> >> >> >>>> illhan
>> >> >> >>>>
>> >> >> >>>
>> >> >> >>>
>> >> >> >>
>> >> >> >>
>> >> >> >
>> >> >> >
>> >> >>
>> >> >> --
>> >> >> View this message in context:
>> >> >>
>> >>
>> http://n2.nabble.com/how-to-use-openjpa-event-listener---tp3397042p3421303.html
>> >> >> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>> >> >>
>> >> >
>> >> >
>> >>
>> >> --
>> >> View this message in context:
>> >>
>> http://n2.nabble.com/how-to-use-openjpa-event-listener---tp3397042p3424516.html
>> >> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>> >>
>> >
>> >
>>
>> --
>> View this message in context:
>> http://n2.nabble.com/how-to-use-openjpa-event-listener---tp3397042p3429428.html
>> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>>
> 
> 

-- 
View this message in context: http://n2.nabble.com/how-to-use-openjpa-event-listener---tp3397042p3456464.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: how to use openjpa event listener ?

Posted by Michael Dick <mi...@gmail.com>.
Hi Ilhan,

Maybe I don't understand the request. Pinaki's
example<http://webspherepersistence.blogspot.com/2009/01/auditing-with-openjpa.html>uses
the JPA spec defined callbacks to get the previous state of the
entity.
I thought the problem you were hitting was the need to put the audit method
in each entity. Using orm.xml you can define a single PostUpdate method
which applies to every entity (of course that audit method may be tightly
coupled to the entities in your PersistenceUnit).

Based on Pinaki's example I think this would work for you.

On to your second question :
I don't think you can inject a PersistenceContext or an EJB into a lifecycle
callback method and you'd want to use a jndi lookup in this case. I'm not an
expert with injection though so I could be proven wrong.

Best Regards,

-mike

On Wed, Aug 12, 2009 at 3:42 AM, illhan <qu...@yahoo.com.cn> wrote:

>
> hi Michael,
> I know the entity lifecyclelistener config in orm.xm,Thanks.
> I think the entity lifecyclelistener is not fit for audit logging.I can not
> get old state from the lifecyclelistener,and the lifecyclelistener is JPA
> spec,not JEE5,so session bean or persistencecontext can not be injected in
> lifecyclelistener,I must use the jndi lookup to find ejb or
> entitymanager,is
> it right?
> Thanks Michael.
> illhan
>
> Michael Dick wrote:
> >
> > Hi Illhan,
> >
> > The audit example from the persistence blog uses a @PostUpdate listener
> to
> > do the auditing. You can specify a default set of listeners for an entire
> > persistence unit in an xml mapping file. For example :
> >
> > persistence.xml :
> >
> > <persistence-unit name="auditable-pu">
> >      <mapping-file>orm.xml</mapping-file>
> > </persistence-unit>
> >
> > orm/xml :
> > <persistence-unit-metadata>
> >    <persistence-unit-defaults>
> >        <entity-listeners>
> >             <entity-listener class="foo.bar.Auditor">
> >                  <pre-persist method-name="prePersist" />
> >                  <post-persist method-name="postPersist" />
> >                  <pre-remove method-name="preRemove" />
> >                  <post-remove method-name="postRemove" />
> >                  <pre-update method-name="preUpdate" />
> >                  <post-update method-name="postUpdate" />
> >                  <post-load method-name="postLoad" />
> >             </entity-listener>
> >         </entity-listeners>
> >    </persistence-unit-defaults>
> > </persistence-unit-metadata>
> >
> >
> > Foo.bar.Auditor.postUpdate() will operate on all the entities in your
> > persistence unit - just like the annotation example in Pinaki's blog
> post.
> >
> > Does that sound like what you want?
> >
> > -mike
> >
> > On Tue, Aug 11, 2009 at 10:19 AM, illhan <qu...@yahoo.com.cn>
> wrote:
> >
> >>
> >> hi Rick,
> >> Because of the audit logging,I used the openJPA LifeCycleListener(maybe
> >> other openjpa listerners) for audit logging.
> >> I have try to record the audit logging by JPA lifecycle callbacks,but in
> >> the
> >> callbacks method's I can not get the old state,I only get the new
> method.
> >> The document
> >>
> http://webspherepersistence.blogspot.com/2009/01/auditing-with-openjpa.html
> >>
> http://webspherepersistence.blogspot.com/2009/01/auditing-with-openjpa.html
> >> shows one method to record audit logging,but  the audit method must be
> >> added
> >> in my all entites ,it is too complex for developers.
> >> in my opinion,the openJPA LifeCycleListener maybe the best way to reduse
> >> the
> >> developers workload,if it can be used here.I can use event descriptor in
> >> eclipselink(toplink),I only to register the event descriptor in
> >> persistence.xml.So I would like to found the similar way in openJPA
> >> environment.
> >>
> >> Thanks Rick.
> >> illhan
> >>
> >> Michael Dick wrote:
> >> >
> >> > Hi Illhan,
> >> > The method you want to use is part of OpenJPA's SPI layer, and isn't
> >> > intended to be used by most applications. As a result the interface
> may
> >> be
> >> > a
> >> > bit more fluid than our 'normal' API methods.
> >> >
> >> > Could you elaborate on why you want to add a LifeCycleListener? We
> >> might
> >> > be
> >> > able to suggest an alternative that accomplishes the same goal..
> >> >
> >> > Best Regards,
> >> > -mike
> >> >
> >> > On Mon, Aug 10, 2009 at 9:05 PM, illhan <qu...@yahoo.com.cn>
> >> wrote:
> >> >
> >> >>
> >> >> hi Rick,
> >> >> You means the JPA lifecycle Callbacks,But I want to know the
> openjpa's
> >> >> listener,in package org.apache.openjpa.event,the interface
> >> >> LifecycleListener
> >> >> and the class LifecycleEvents.
> >> >> From the javadoc,the LifecycleListener  can be registered by the
> >> method
> >> >> addLifecycleListener(Object listener, Class... classes) from class
> >> >> org.apache.openjpa.persistence.EntityManagerImpl,but in JEE container
> >> the
> >> >> EntityManager is inited by the container itself.How can I registere
> >> the
> >> >> LifecycleListener and process the LifecycleEvents.I have reviewed the
> >> >> openjpa user's guide,but no part can be found there.
> >> >> Thanks Rick.
> >> >> illhan
> >> >>
> >> >> Rick Curtis wrote:
> >> >> >
> >> >> > Illhan -
> >> >> >
> >> >> > Did you read the section in the user manual detailing the usage of
> >> >> > Lifecycle Callbacks [1]?
> >> >> >
> >> >> > -Rick
> >> >> >
> >> >> > [1]
> >> >>
> >>
> http://openjpa.apache.org/builds/latest/docs/manual/manual.html#jpa_overview_pc_callbacks
> >> >> >
> >> >> >
> >> >> > illhan wrote:
> >> >> >>
> >> >> >> hi Rick,
> >> >> >> The EntityManager and the EntityManagerFactory are initializated
> by
> >> >> the
> >> >> >> JEE contianer.I think the LifecycleListener could be set in
> >> >> >> persistent.xml,not only used by java code.
> >> >> >> Thanks Rick.
> >> >> >>
> >> >> >> illhan
> >> >> >>
> >> >> >> Rick Curtis wrote:
> >> >> >>>
> >> >> >>> Illhan -
> >> >> >>> In your application initialization I believe you could do
> >> something
> >> >> like
> >> >> >>> this to get a reference to the EntityManagerFactory....
> >> >> >>>
> >> >> >>>             OpenJPAEntityManager oem =
> >> OpenJPAPersistence.cast(em);
> >> >> >>>             OpenJPAEntityManagerFactory oemf =
> >> >> >>> OpenJPAPersistence.cast(oem.getEntityManagerFactory());
> >> >> >>>
> >> >> >>> - Rick
> >> >> >>>
> >> >> >>> illhan wrote:
> >> >> >>>>
> >> >> >>>> In the javadoc there are many event and listener.I want to use
> >> the
> >> >> >>>> event and the listener in jee enviroment,the emf.add**Listener()
> >> >> method
> >> >> >>>> can't not called because in jee enviroment the
> >> entitymanagerfacetory
> >> >> is
> >> >> >>>> created by jee contianer.i only use ejb3 injection in my session
> >> >> bean
> >> >> >>>> use   @PersistenceContext
> >> >> >>>>     private EntityManager em;
> >> >> >>>> so i don't know how to register the Listener(such as
> >> >> LifecycleListener)
> >> >> >>>> in JEE enviroment.
> >> >> >>>>
> >> >> >>>> illhan
> >> >> >>>>
> >> >> >>>
> >> >> >>>
> >> >> >>
> >> >> >>
> >> >> >
> >> >> >
> >> >>
> >> >> --
> >> >> View this message in context:
> >> >>
> >>
> http://n2.nabble.com/how-to-use-openjpa-event-listener---tp3397042p3421303.html
> >> >> Sent from the OpenJPA Users mailing list archive at Nabble.com.
> >> >>
> >> >
> >> >
> >>
> >> --
> >> View this message in context:
> >>
> http://n2.nabble.com/how-to-use-openjpa-event-listener---tp3397042p3424516.html
> >> Sent from the OpenJPA Users mailing list archive at Nabble.com.
> >>
> >
> >
>
> --
> View this message in context:
> http://n2.nabble.com/how-to-use-openjpa-event-listener---tp3397042p3429428.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>

Re: how to use openjpa event listener ?

Posted by illhan <qu...@yahoo.com.cn>.
hi Michael,
I know the entity lifecyclelistener config in orm.xm,Thanks.
I think the entity lifecyclelistener is not fit for audit logging.I can not
get old state from the lifecyclelistener,and the lifecyclelistener is JPA
spec,not JEE5,so session bean or persistencecontext can not be injected in
lifecyclelistener,I must use the jndi lookup to find ejb or entitymanager,is
it right?
Thanks Michael.
illhan

Michael Dick wrote:
> 
> Hi Illhan,
> 
> The audit example from the persistence blog uses a @PostUpdate listener to
> do the auditing. You can specify a default set of listeners for an entire
> persistence unit in an xml mapping file. For example :
> 
> persistence.xml :
> 
> <persistence-unit name="auditable-pu">
>      <mapping-file>orm.xml</mapping-file>
> </persistence-unit>
> 
> orm/xml :
> <persistence-unit-metadata>
>    <persistence-unit-defaults>
>        <entity-listeners>
>             <entity-listener class="foo.bar.Auditor">
>                  <pre-persist method-name="prePersist" />
>                  <post-persist method-name="postPersist" />
>                  <pre-remove method-name="preRemove" />
>                  <post-remove method-name="postRemove" />
>                  <pre-update method-name="preUpdate" />
>                  <post-update method-name="postUpdate" />
>                  <post-load method-name="postLoad" />
>             </entity-listener>
>         </entity-listeners>
>    </persistence-unit-defaults>
> </persistence-unit-metadata>
> 
> 
> Foo.bar.Auditor.postUpdate() will operate on all the entities in your
> persistence unit - just like the annotation example in Pinaki's blog post.
> 
> Does that sound like what you want?
> 
> -mike
> 
> On Tue, Aug 11, 2009 at 10:19 AM, illhan <qu...@yahoo.com.cn> wrote:
> 
>>
>> hi Rick,
>> Because of the audit logging,I used the openJPA LifeCycleListener(maybe
>> other openjpa listerners) for audit logging.
>> I have try to record the audit logging by JPA lifecycle callbacks,but in
>> the
>> callbacks method's I can not get the old state,I only get the new method.
>> The document
>> http://webspherepersistence.blogspot.com/2009/01/auditing-with-openjpa.html
>> http://webspherepersistence.blogspot.com/2009/01/auditing-with-openjpa.html
>> shows one method to record audit logging,but  the audit method must be
>> added
>> in my all entites ,it is too complex for developers.
>> in my opinion,the openJPA LifeCycleListener maybe the best way to reduse
>> the
>> developers workload,if it can be used here.I can use event descriptor in
>> eclipselink(toplink),I only to register the event descriptor in
>> persistence.xml.So I would like to found the similar way in openJPA
>> environment.
>>
>> Thanks Rick.
>> illhan
>>
>> Michael Dick wrote:
>> >
>> > Hi Illhan,
>> > The method you want to use is part of OpenJPA's SPI layer, and isn't
>> > intended to be used by most applications. As a result the interface may
>> be
>> > a
>> > bit more fluid than our 'normal' API methods.
>> >
>> > Could you elaborate on why you want to add a LifeCycleListener? We
>> might
>> > be
>> > able to suggest an alternative that accomplishes the same goal..
>> >
>> > Best Regards,
>> > -mike
>> >
>> > On Mon, Aug 10, 2009 at 9:05 PM, illhan <qu...@yahoo.com.cn>
>> wrote:
>> >
>> >>
>> >> hi Rick,
>> >> You means the JPA lifecycle Callbacks,But I want to know the openjpa's
>> >> listener,in package org.apache.openjpa.event,the interface
>> >> LifecycleListener
>> >> and the class LifecycleEvents.
>> >> From the javadoc,the LifecycleListener  can be registered by the
>> method
>> >> addLifecycleListener(Object listener, Class... classes) from class
>> >> org.apache.openjpa.persistence.EntityManagerImpl,but in JEE container
>> the
>> >> EntityManager is inited by the container itself.How can I registere
>> the
>> >> LifecycleListener and process the LifecycleEvents.I have reviewed the
>> >> openjpa user's guide,but no part can be found there.
>> >> Thanks Rick.
>> >> illhan
>> >>
>> >> Rick Curtis wrote:
>> >> >
>> >> > Illhan -
>> >> >
>> >> > Did you read the section in the user manual detailing the usage of
>> >> > Lifecycle Callbacks [1]?
>> >> >
>> >> > -Rick
>> >> >
>> >> > [1]
>> >>
>> http://openjpa.apache.org/builds/latest/docs/manual/manual.html#jpa_overview_pc_callbacks
>> >> >
>> >> >
>> >> > illhan wrote:
>> >> >>
>> >> >> hi Rick,
>> >> >> The EntityManager and the EntityManagerFactory are initializated by
>> >> the
>> >> >> JEE contianer.I think the LifecycleListener could be set in
>> >> >> persistent.xml,not only used by java code.
>> >> >> Thanks Rick.
>> >> >>
>> >> >> illhan
>> >> >>
>> >> >> Rick Curtis wrote:
>> >> >>>
>> >> >>> Illhan -
>> >> >>> In your application initialization I believe you could do
>> something
>> >> like
>> >> >>> this to get a reference to the EntityManagerFactory....
>> >> >>>
>> >> >>>             OpenJPAEntityManager oem =
>> OpenJPAPersistence.cast(em);
>> >> >>>             OpenJPAEntityManagerFactory oemf =
>> >> >>> OpenJPAPersistence.cast(oem.getEntityManagerFactory());
>> >> >>>
>> >> >>> - Rick
>> >> >>>
>> >> >>> illhan wrote:
>> >> >>>>
>> >> >>>> In the javadoc there are many event and listener.I want to use
>> the
>> >> >>>> event and the listener in jee enviroment,the emf.add**Listener()
>> >> method
>> >> >>>> can't not called because in jee enviroment the
>> entitymanagerfacetory
>> >> is
>> >> >>>> created by jee contianer.i only use ejb3 injection in my session
>> >> bean
>> >> >>>> use   @PersistenceContext
>> >> >>>>     private EntityManager em;
>> >> >>>> so i don't know how to register the Listener(such as
>> >> LifecycleListener)
>> >> >>>> in JEE enviroment.
>> >> >>>>
>> >> >>>> illhan
>> >> >>>>
>> >> >>>
>> >> >>>
>> >> >>
>> >> >>
>> >> >
>> >> >
>> >>
>> >> --
>> >> View this message in context:
>> >>
>> http://n2.nabble.com/how-to-use-openjpa-event-listener---tp3397042p3421303.html
>> >> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>> >>
>> >
>> >
>>
>> --
>> View this message in context:
>> http://n2.nabble.com/how-to-use-openjpa-event-listener---tp3397042p3424516.html
>> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>>
> 
> 

-- 
View this message in context: http://n2.nabble.com/how-to-use-openjpa-event-listener---tp3397042p3429428.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: how to use openjpa event listener ?

Posted by Michael Dick <mi...@gmail.com>.
Hi Illhan,

The audit example from the persistence blog uses a @PostUpdate listener to
do the auditing. You can specify a default set of listeners for an entire
persistence unit in an xml mapping file. For example :

persistence.xml :

<persistence-unit name="auditable-pu">
     <mapping-file>orm.xml</mapping-file>
</persistence-unit>

orm/xml :
<persistence-unit-metadata>
   <persistence-unit-defaults>
       <entity-listeners>
            <entity-listener class="foo.bar.Auditor">
                 <pre-persist method-name="prePersist" />
                 <post-persist method-name="postPersist" />
                 <pre-remove method-name="preRemove" />
                 <post-remove method-name="postRemove" />
                 <pre-update method-name="preUpdate" />
                 <post-update method-name="postUpdate" />
                 <post-load method-name="postLoad" />
            </entity-listener>
        </entity-listeners>
   </persistence-unit-defaults>
</persistence-unit-metadata>


Foo.bar.Auditor.postUpdate() will operate on all the entities in your
persistence unit - just like the annotation example in Pinaki's blog post.

Does that sound like what you want?

-mike

On Tue, Aug 11, 2009 at 10:19 AM, illhan <qu...@yahoo.com.cn> wrote:

>
> hi Rick,
> Because of the audit logging,I used the openJPA LifeCycleListener(maybe
> other openjpa listerners) for audit logging.
> I have try to record the audit logging by JPA lifecycle callbacks,but in
> the
> callbacks method's I can not get the old state,I only get the new method.
> The document
> http://webspherepersistence.blogspot.com/2009/01/auditing-with-openjpa.html
> http://webspherepersistence.blogspot.com/2009/01/auditing-with-openjpa.html
> shows one method to record audit logging,but  the audit method must be
> added
> in my all entites ,it is too complex for developers.
> in my opinion,the openJPA LifeCycleListener maybe the best way to reduse
> the
> developers workload,if it can be used here.I can use event descriptor in
> eclipselink(toplink),I only to register the event descriptor in
> persistence.xml.So I would like to found the similar way in openJPA
> environment.
>
> Thanks Rick.
> illhan
>
> Michael Dick wrote:
> >
> > Hi Illhan,
> > The method you want to use is part of OpenJPA's SPI layer, and isn't
> > intended to be used by most applications. As a result the interface may
> be
> > a
> > bit more fluid than our 'normal' API methods.
> >
> > Could you elaborate on why you want to add a LifeCycleListener? We might
> > be
> > able to suggest an alternative that accomplishes the same goal..
> >
> > Best Regards,
> > -mike
> >
> > On Mon, Aug 10, 2009 at 9:05 PM, illhan <qu...@yahoo.com.cn> wrote:
> >
> >>
> >> hi Rick,
> >> You means the JPA lifecycle Callbacks,But I want to know the openjpa's
> >> listener,in package org.apache.openjpa.event,the interface
> >> LifecycleListener
> >> and the class LifecycleEvents.
> >> From the javadoc,the LifecycleListener  can be registered by the method
> >> addLifecycleListener(Object listener, Class... classes) from class
> >> org.apache.openjpa.persistence.EntityManagerImpl,but in JEE container
> the
> >> EntityManager is inited by the container itself.How can I registere the
> >> LifecycleListener and process the LifecycleEvents.I have reviewed the
> >> openjpa user's guide,but no part can be found there.
> >> Thanks Rick.
> >> illhan
> >>
> >> Rick Curtis wrote:
> >> >
> >> > Illhan -
> >> >
> >> > Did you read the section in the user manual detailing the usage of
> >> > Lifecycle Callbacks [1]?
> >> >
> >> > -Rick
> >> >
> >> > [1]
> >>
> http://openjpa.apache.org/builds/latest/docs/manual/manual.html#jpa_overview_pc_callbacks
> >> >
> >> >
> >> > illhan wrote:
> >> >>
> >> >> hi Rick,
> >> >> The EntityManager and the EntityManagerFactory are initializated by
> >> the
> >> >> JEE contianer.I think the LifecycleListener could be set in
> >> >> persistent.xml,not only used by java code.
> >> >> Thanks Rick.
> >> >>
> >> >> illhan
> >> >>
> >> >> Rick Curtis wrote:
> >> >>>
> >> >>> Illhan -
> >> >>> In your application initialization I believe you could do something
> >> like
> >> >>> this to get a reference to the EntityManagerFactory....
> >> >>>
> >> >>>             OpenJPAEntityManager oem = OpenJPAPersistence.cast(em);
> >> >>>             OpenJPAEntityManagerFactory oemf =
> >> >>> OpenJPAPersistence.cast(oem.getEntityManagerFactory());
> >> >>>
> >> >>> - Rick
> >> >>>
> >> >>> illhan wrote:
> >> >>>>
> >> >>>> In the javadoc there are many event and listener.I want to use the
> >> >>>> event and the listener in jee enviroment,the emf.add**Listener()
> >> method
> >> >>>> can't not called because in jee enviroment the
> entitymanagerfacetory
> >> is
> >> >>>> created by jee contianer.i only use ejb3 injection in my session
> >> bean
> >> >>>> use   @PersistenceContext
> >> >>>>     private EntityManager em;
> >> >>>> so i don't know how to register the Listener(such as
> >> LifecycleListener)
> >> >>>> in JEE enviroment.
> >> >>>>
> >> >>>> illhan
> >> >>>>
> >> >>>
> >> >>>
> >> >>
> >> >>
> >> >
> >> >
> >>
> >> --
> >> View this message in context:
> >>
> http://n2.nabble.com/how-to-use-openjpa-event-listener---tp3397042p3421303.html
> >> Sent from the OpenJPA Users mailing list archive at Nabble.com.
> >>
> >
> >
>
> --
> View this message in context:
> http://n2.nabble.com/how-to-use-openjpa-event-listener---tp3397042p3424516.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>

Re: how to use openjpa event listener ?

Posted by illhan <qu...@yahoo.com.cn>.
hi Rick,
Because of the audit logging,I used the openJPA LifeCycleListener(maybe
other openjpa listerners) for audit logging.
I have try to record the audit logging by JPA lifecycle callbacks,but in the
callbacks method's I can not get the old state,I only get the new method.
The document 
http://webspherepersistence.blogspot.com/2009/01/auditing-with-openjpa.html
http://webspherepersistence.blogspot.com/2009/01/auditing-with-openjpa.html 
shows one method to record audit logging,but  the audit method must be added 
in my all entites ,it is too complex for developers.
in my opinion,the openJPA LifeCycleListener maybe the best way to reduse the
developers workload,if it can be used here.I can use event descriptor in
eclipselink(toplink),I only to register the event descriptor in
persistence.xml.So I would like to found the similar way in openJPA
environment.

Thanks Rick.
illhan

Michael Dick wrote:
> 
> Hi Illhan,
> The method you want to use is part of OpenJPA's SPI layer, and isn't
> intended to be used by most applications. As a result the interface may be
> a
> bit more fluid than our 'normal' API methods.
> 
> Could you elaborate on why you want to add a LifeCycleListener? We might
> be
> able to suggest an alternative that accomplishes the same goal..
> 
> Best Regards,
> -mike
> 
> On Mon, Aug 10, 2009 at 9:05 PM, illhan <qu...@yahoo.com.cn> wrote:
> 
>>
>> hi Rick,
>> You means the JPA lifecycle Callbacks,But I want to know the openjpa's
>> listener,in package org.apache.openjpa.event,the interface
>> LifecycleListener
>> and the class LifecycleEvents.
>> From the javadoc,the LifecycleListener  can be registered by the method
>> addLifecycleListener(Object listener, Class... classes) from class
>> org.apache.openjpa.persistence.EntityManagerImpl,but in JEE container the
>> EntityManager is inited by the container itself.How can I registere the
>> LifecycleListener and process the LifecycleEvents.I have reviewed the
>> openjpa user's guide,but no part can be found there.
>> Thanks Rick.
>> illhan
>>
>> Rick Curtis wrote:
>> >
>> > Illhan -
>> >
>> > Did you read the section in the user manual detailing the usage of
>> > Lifecycle Callbacks [1]?
>> >
>> > -Rick
>> >
>> > [1]
>> http://openjpa.apache.org/builds/latest/docs/manual/manual.html#jpa_overview_pc_callbacks
>> >
>> >
>> > illhan wrote:
>> >>
>> >> hi Rick,
>> >> The EntityManager and the EntityManagerFactory are initializated by
>> the
>> >> JEE contianer.I think the LifecycleListener could be set in
>> >> persistent.xml,not only used by java code.
>> >> Thanks Rick.
>> >>
>> >> illhan
>> >>
>> >> Rick Curtis wrote:
>> >>>
>> >>> Illhan -
>> >>> In your application initialization I believe you could do something
>> like
>> >>> this to get a reference to the EntityManagerFactory....
>> >>>
>> >>>             OpenJPAEntityManager oem = OpenJPAPersistence.cast(em);
>> >>>             OpenJPAEntityManagerFactory oemf =
>> >>> OpenJPAPersistence.cast(oem.getEntityManagerFactory());
>> >>>
>> >>> - Rick
>> >>>
>> >>> illhan wrote:
>> >>>>
>> >>>> In the javadoc there are many event and listener.I want to use the
>> >>>> event and the listener in jee enviroment,the emf.add**Listener()
>> method
>> >>>> can't not called because in jee enviroment the entitymanagerfacetory
>> is
>> >>>> created by jee contianer.i only use ejb3 injection in my session
>> bean
>> >>>> use   @PersistenceContext
>> >>>>     private EntityManager em;
>> >>>> so i don't know how to register the Listener(such as
>> LifecycleListener)
>> >>>> in JEE enviroment.
>> >>>>
>> >>>> illhan
>> >>>>
>> >>>
>> >>>
>> >>
>> >>
>> >
>> >
>>
>> --
>> View this message in context:
>> http://n2.nabble.com/how-to-use-openjpa-event-listener---tp3397042p3421303.html
>> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>>
> 
> 

-- 
View this message in context: http://n2.nabble.com/how-to-use-openjpa-event-listener---tp3397042p3424516.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: how to use openjpa event listener ?

Posted by Michael Dick <mi...@gmail.com>.
Hi Illhan,

The method you want to use is part of OpenJPA's SPI layer, and isn't
intended to be used by most applications. As a result the interface may be a
bit more fluid than our 'normal' API methods.

Could you elaborate on why you want to add a LifeCycleListener? We might be
able to suggest an alternative that accomplishes the same goal..

Best Regards,
-mike

On Mon, Aug 10, 2009 at 9:05 PM, illhan <qu...@yahoo.com.cn> wrote:

>
> hi Rick,
> You means the JPA lifecycle Callbacks,But I want to know the openjpa's
> listener,in package org.apache.openjpa.event,the interface
> LifecycleListener
> and the class LifecycleEvents.
> From the javadoc,the LifecycleListener  can be registered by the method
> addLifecycleListener(Object listener, Class... classes) from class
> org.apache.openjpa.persistence.EntityManagerImpl,but in JEE container the
> EntityManager is inited by the container itself.How can I registere the
> LifecycleListener and process the LifecycleEvents.I have reviewed the
> openjpa user's guide,but no part can be found there.
> Thanks Rick.
> illhan
>
> Rick Curtis wrote:
> >
> > Illhan -
> >
> > Did you read the section in the user manual detailing the usage of
> > Lifecycle Callbacks [1]?
> >
> > -Rick
> >
> > [1]
> http://openjpa.apache.org/builds/latest/docs/manual/manual.html#jpa_overview_pc_callbacks
> >
> >
> > illhan wrote:
> >>
> >> hi Rick,
> >> The EntityManager and the EntityManagerFactory are initializated by the
> >> JEE contianer.I think the LifecycleListener could be set in
> >> persistent.xml,not only used by java code.
> >> Thanks Rick.
> >>
> >> illhan
> >>
> >> Rick Curtis wrote:
> >>>
> >>> Illhan -
> >>> In your application initialization I believe you could do something
> like
> >>> this to get a reference to the EntityManagerFactory....
> >>>
> >>>             OpenJPAEntityManager oem = OpenJPAPersistence.cast(em);
> >>>             OpenJPAEntityManagerFactory oemf =
> >>> OpenJPAPersistence.cast(oem.getEntityManagerFactory());
> >>>
> >>> - Rick
> >>>
> >>> illhan wrote:
> >>>>
> >>>> In the javadoc there are many event and listener.I want to use the
> >>>> event and the listener in jee enviroment,the emf.add**Listener()
> method
> >>>> can't not called because in jee enviroment the entitymanagerfacetory
> is
> >>>> created by jee contianer.i only use ejb3 injection in my session bean
> >>>> use   @PersistenceContext
> >>>>     private EntityManager em;
> >>>> so i don't know how to register the Listener(such as
> LifecycleListener)
> >>>> in JEE enviroment.
> >>>>
> >>>> illhan
> >>>>
> >>>
> >>>
> >>
> >>
> >
> >
>
> --
> View this message in context:
> http://n2.nabble.com/how-to-use-openjpa-event-listener---tp3397042p3421303.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
>

Re: how to use openjpa event listener ?

Posted by illhan <qu...@yahoo.com.cn>.
hi Rick,
You means the JPA lifecycle Callbacks,But I want to know the openjpa's
listener,in package org.apache.openjpa.event,the interface LifecycleListener
and the class LifecycleEvents.
>From the javadoc,the LifecycleListener  can be registered by the method
addLifecycleListener(Object listener, Class... classes) from class
org.apache.openjpa.persistence.EntityManagerImpl,but in JEE container the
EntityManager is inited by the container itself.How can I registere the
LifecycleListener and process the LifecycleEvents.I have reviewed the
openjpa user's guide,but no part can be found there.
Thanks Rick.
illhan

Rick Curtis wrote:
> 
> Illhan -
> 
> Did you read the section in the user manual detailing the usage of
> Lifecycle Callbacks [1]?
> 
> -Rick
> 
> [1]http://openjpa.apache.org/builds/latest/docs/manual/manual.html#jpa_overview_pc_callbacks
> 
> 
> illhan wrote:
>> 
>> hi Rick,
>> The EntityManager and the EntityManagerFactory are initializated by the
>> JEE contianer.I think the LifecycleListener could be set in
>> persistent.xml,not only used by java code.
>> Thanks Rick.
>> 
>> illhan
>> 
>> Rick Curtis wrote:
>>> 
>>> Illhan -
>>> In your application initialization I believe you could do something like
>>> this to get a reference to the EntityManagerFactory....
>>> 
>>> 		OpenJPAEntityManager oem = OpenJPAPersistence.cast(em);
>>> 		OpenJPAEntityManagerFactory oemf =
>>> OpenJPAPersistence.cast(oem.getEntityManagerFactory());
>>> 
>>> - Rick
>>> 
>>> illhan wrote:
>>>> 
>>>> In the javadoc there are many event and listener.I want to use the
>>>> event and the listener in jee enviroment,the emf.add**Listener() method
>>>> can't not called because in jee enviroment the entitymanagerfacetory is
>>>> created by jee contianer.i only use ejb3 injection in my session bean
>>>> use   @PersistenceContext
>>>>     private EntityManager em;
>>>> so i don't know how to register the Listener(such as LifecycleListener)
>>>> in JEE enviroment.
>>>> 
>>>> illhan
>>>> 
>>> 
>>> 
>> 
>> 
> 
> 

-- 
View this message in context: http://n2.nabble.com/how-to-use-openjpa-event-listener---tp3397042p3421303.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: how to use openjpa event listener ?

Posted by Rick Curtis <cu...@gmail.com>.
Illhan -

Did you read the section in the user manual detailing the usage of Lifecycle
Callbacks [1]?

-Rick

[1]http://openjpa.apache.org/builds/latest/docs/manual/manual.html#jpa_overview_pc_callbacks


illhan wrote:
> 
> hi Rick,
> The EntityManager and the EntityManagerFactory are initializated by the
> JEE contianer.I think the LifecycleListener could be set in
> persistent.xml,not only used by java code.
> Thanks Rick.
> 
> illhan
> 
> Rick Curtis wrote:
>> 
>> Illhan -
>> In your application initialization I believe you could do something like
>> this to get a reference to the EntityManagerFactory....
>> 
>> 		OpenJPAEntityManager oem = OpenJPAPersistence.cast(em);
>> 		OpenJPAEntityManagerFactory oemf =
>> OpenJPAPersistence.cast(oem.getEntityManagerFactory());
>> 
>> - Rick
>> 
>> illhan wrote:
>>> 
>>> In the javadoc there are many event and listener.I want to use the event
>>> and the listener in jee enviroment,the emf.add**Listener() method can't
>>> not called because in jee enviroment the entitymanagerfacetory is
>>> created by jee contianer.i only use ejb3 injection in my session bean
>>> use   @PersistenceContext
>>>     private EntityManager em;
>>> so i don't know how to register the Listener(such as LifecycleListener)
>>> in JEE enviroment.
>>> 
>>> illhan
>>> 
>> 
>> 
> 
> 

-- 
View this message in context: http://n2.nabble.com/how-to-use-openjpa-event-listener---tp3397042p3417396.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: how to use openjpa event listener ?

Posted by illhan <qu...@yahoo.com.cn>.
hi Rick,
The EntityManager and the EntityManagerFactory are initializated by the JEE
contianer.I think the LifecycleListener could be set in persistent.xml,not
only used by java code.
Thanks Rick.

illhan

Rick Curtis wrote:
> 
> Illhan -
> In your application initialization I believe you could do something like
> this to get a reference to the EntityManagerFactory....
> 
> 		OpenJPAEntityManager oem = OpenJPAPersistence.cast(em);
> 		OpenJPAEntityManagerFactory oemf =
> OpenJPAPersistence.cast(oem.getEntityManagerFactory());
> 
> - Rick
> 
> illhan wrote:
>> 
>> In the javadoc there are many event and listener.I want to use the event
>> and the listener in jee enviroment,the emf.add**Listener() method can't
>> not called because in jee enviroment the entitymanagerfacetory is created
>> by jee contianer.i only use ejb3 injection in my session bean use  
>> @PersistenceContext
>>     private EntityManager em;
>> so i don't know how to register the Listener(such as LifecycleListener)
>> in JEE enviroment.
>> 
>> illhan
>> 
> 
> 

-- 
View this message in context: http://n2.nabble.com/how-to-use-openjpa-event-listener---tp3397042p3415530.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.

Re: how to use openjpa event listener ?

Posted by Rick Curtis <cu...@gmail.com>.
Illhan -
In your application initialization I believe you could do something like
this to get a reference to the EntityManagerFactory....

		OpenJPAEntityManager oem = OpenJPAPersistence.cast(em);
		OpenJPAEntityManagerFactory oemf =
OpenJPAPersistence.cast(oem.getEntityManagerFactory());

- Rick

illhan wrote:
> 
> In the javadoc there are many event and listener.I want to use the event
> and the listener in jee enviroment,the emf.add**Listener() method can't
> not called because in jee enviroment the entitymanagerfacetory is created
> by jee contianer.i only use ejb3 injection in my session bean use  
> @PersistenceContext
>     private EntityManager em;
> so i don't know how to register the Listener(such as LifecycleListener) in
> JEE enviroment.
> 
> illhan
> 

-- 
View this message in context: http://n2.nabble.com/how-to-use-openjpa-event-listener---tp3397042p3400138.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.