You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Klesk <cg...@hotmail.com> on 2017/07/04 08:18:24 UTC
Attribut change listener
Hi,
Is there a way to implements an attribute listener ?
My goal is to be notified when an attribute content changed (getting
attribute name, old and new value).
Is is feasible ?
Regards,
--
View this message in context: http://openjpa.208410.n2.nabble.com/Attribut-change-listener-tp7590226.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.
RE: Attribut change listener
Posted by Boblitz John <jo...@bertschi.com>.
In that case, why not just implement notification in the setter for the attribute?
public void setSomeAttribute(Object someNewValue) {
Object oldValue = attributeValue;
attibuteValue = someNewValue;
System.out.println("changed: " + oldValue + " -> " attributeValue);
}
John
> -----Original Message-----
> From: Klesk [mailto:cg85@hotmail.com]
> Sent: Dienstag, 4. Juli 2017 12:11
> To: users@openjpa.apache.org
> Subject: RE: Attribut change listener
>
> Hello,
>
> In fact, I need to be notified when an attribute content change ASAP.
> Methods with an event annotation are called when the EntityManager is
> called. That's mhy I don't use annotations ;)
>
> Regards,
>
>
>
> --
> View this message in context:
> http://openjpa.208410.n2.nabble.com/Attribut-change-listener-
> tp7590226p7590229.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
RE: Attribut change listener
Posted by Klesk <cg...@hotmail.com>.
Hello,
In fact, I need to be notified when an attribute content change ASAP.
Methods with an event annotation are called when the EntityManager is
called. That's mhy I don't use annotations ;)
Regards,
--
View this message in context: http://openjpa.208410.n2.nabble.com/Attribut-change-listener-tp7590226p7590229.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.
RE: Attribut change listener
Posted by Boblitz John <jo...@bertschi.com>.
Hello,
You can create a method and annotate it with @PostPersist @PostUpdate @PostRemove (also pre) depending on your needs.
Then access the old and new states of the entity with
PersistenceCapable pcCurrentState = (PersistenceCapable) pCurrentState;
StateManagerImpl sm = (StateManagerImpl) pcCurrentState.pcGetStateManager();
PCState state = sm.getPCState();
SaveFieldManager sfm = sm.getSaveFieldManager();
PersistenceCapable pcOldState = sfm.getState();
You then cast the states to your entity
oldState = (Entity) pcOldState;
newState = (Entity) pcCurrentState;
I do believe you need to add something to your persistence.xml to allow this to work, but I don't recall for certain.
Hope this helps
John
> -----Original Message-----
> From: Klesk [mailto:cg85@hotmail.com]
> Sent: Dienstag, 4. Juli 2017 11:14
> To: users@openjpa.apache.org
> Subject: Re: Attribut change listener
>
> My workaround (needs to be deeply tested):
>
>
> Klesk wrote
> > @Test
> > public void testProxy() throws Exception {
> > final EntityManager em = factory.createEntityManager();
> > final SUti entity = em.find(SUti.class, 1);
> >
> > final ProxyFactory proxiter = new ProxyFactory();
> > proxiter.setSuperclass(entity.getClass());
> > proxiter.setHandler((instance, method, wrapper, args) -> {
> > instance = entity;
> > wrapper = entity.getClass().getMethod(method.getName(),
> > method.getParameterTypes());
> > if(method.getName().startsWith("set")) {
> > final String methodName = method.getName();
> > final Object previous =
> > instance.getClass().getMethod("get" +
> > methodName.substring(3)).invoke(instance);
> > System.out.println("changed: " + previous + " -> " +
> > args[0]);
> > }
> > return wrapper.invoke(instance, args);
> > });
> >
> > final SUti mock = (SUti) proxiter.create(new Class[0], new
> > Object[0]);
> > assertEquals("AAAA", entity.getUtLibuti());
> > assertEquals("AAAA", mock.getUtLibuti());
> > mock.setUtLibuti("test");
> > assertEquals("test", entity.getUtLibuti());
> > assertEquals("test", mock.getUtLibuti());
> > }
>
>
>
>
>
> --
> View this message in context:
> http://openjpa.208410.n2.nabble.com/Attribut-change-listener-
> tp7590226p7590227.html
> Sent from the OpenJPA Users mailing list archive at Nabble.com.
Re: Attribut change listener
Posted by Klesk <cg...@hotmail.com>.
My workaround (needs to be deeply tested):
Klesk wrote
> @Test
> public void testProxy() throws Exception {
> final EntityManager em = factory.createEntityManager();
> final SUti entity = em.find(SUti.class, 1);
>
> final ProxyFactory proxiter = new ProxyFactory();
> proxiter.setSuperclass(entity.getClass());
> proxiter.setHandler((instance, method, wrapper, args) -> {
> instance = entity;
> wrapper = entity.getClass().getMethod(method.getName(),
> method.getParameterTypes());
> if(method.getName().startsWith("set")) {
> final String methodName = method.getName();
> final Object previous =
> instance.getClass().getMethod("get" +
> methodName.substring(3)).invoke(instance);
> System.out.println("changed: " + previous + " -> " +
> args[0]);
> }
> return wrapper.invoke(instance, args);
> });
>
> final SUti mock = (SUti) proxiter.create(new Class[0], new
> Object[0]);
> assertEquals("AAAA", entity.getUtLibuti());
> assertEquals("AAAA", mock.getUtLibuti());
> mock.setUtLibuti("test");
> assertEquals("test", entity.getUtLibuti());
> assertEquals("test", mock.getUtLibuti());
> }
--
View this message in context: http://openjpa.208410.n2.nabble.com/Attribut-change-listener-tp7590226p7590227.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.