-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: PreUpdateEventListener allowed to modify the object ?
PostPosted: Thu May 07, 2009 9:06 am 
Beginner
Beginner

Joined: Wed Feb 23, 2005 11:32 am
Posts: 25
Location: Austria/Vienna
is a PreUpdateEventListener allowed to modify the object ?

more generally are event listeners allowed to modify objects, and are those changes persisted ?


Top
 Profile  
 
 Post subject: Re: PreUpdateEventListener allowed to modify the object ?
PostPosted: Thu Feb 11, 2010 6:51 pm 
Newbie

Joined: Fri Jun 19, 2009 9:31 am
Posts: 12
Same question here. All my investigations turned out that modifying that all modifications to the object are not persisted in the database.
I tried out for example an PreUpdateEventListener. The listeners method "onPreUpdate" is called, but any changes to the objects are ignored.
My listener looks like this:
Code:
[...]
public class MyListener implements PreUpdateEventListener {
   @Override
   public boolean onPreUpdate(PreUpdateEvent event) {
      System.out.println("Listener was called!");
      if (event.getEntity() instanceof MyObject) {
         ((MyObject)event.getEntity()).setAttribute("new value");
      }
      return false;
   }   
}

Generally, hibernate's complete event listener system seems to be very rarely documented. Or do I miss something? For example finding out that the return value of the onPreUpdate says whether or not to veto the operation needs some research.
Since the method is call PRE-update it's *very confusing*, that the changes are ignored. Moreover: JPA's counterpart @PreUpdate works as expected: All changes to the object are persisted in the database.

Same/similar problem is described here: viewtopic.php?t=953934

IMHO, the only way to change the entitie's state before persisting in pure hibernate (without JPA container) is to use hibernate's interceptors and use the Object[] state and String[] propertyNames values passed to onSave(..) or onFlushDirty(..) which is some kind of cumbersome ...

Any comments?


Top
 Profile  
 
 Post subject: Re: PreUpdateEventListener allowed to modify the object ?
PostPosted: Fri Feb 12, 2010 3:45 am 
Newbie

Joined: Fri Jun 19, 2009 9:31 am
Posts: 12
Even more confusing is, that using a PreInsertEventListener modifying the object is possible: all changed properties/attributes are persisted to the database. Of course this only works for new entities and not for updating existing ones.


Top
 Profile  
 
 Post subject: Re: PreUpdateEventListener allowed to modify the object ?
PostPosted: Tue Mar 23, 2010 11:41 am 
Newbie

Joined: Wed May 11, 2005 7:16 am
Posts: 9
Is it possible that someone could explain the idea behind this behaviour?


Top
 Profile  
 
 Post subject: Re: PreUpdateEventListener allowed to modify the object ?
PostPosted: Mon Apr 12, 2010 6:20 am 
Newbie

Joined: Wed May 11, 2005 7:16 am
Posts: 9
Ok..may be i should ask more detailed...

Why is it possible to modify values in PreInsertEventListener and this Values would persist, and if i'll try this in PreUpdateEventLister these changes will ignored? Is this a bug or a feature? ;-)


Top
 Profile  
 
 Post subject: Re: PreUpdateEventListener allowed to modify the object ?
PostPosted: Sun Nov 21, 2010 5:43 pm 
Newbie

Joined: Tue May 18, 2010 5:39 am
Posts: 19
I had the same problem and was able to solve it using Listeners. I describe this in my blog at http://anshuiitk.blogspot.com/2010/11/h ... event.html . Let me know in case you have any doubts which I can clarify after reading the post.

_________________
AG
http://anshuiitk.blogspot.com


Top
 Profile  
 
 Post subject: Re: PreUpdateEventListener allowed to modify the object ?
PostPosted: Fri Aug 19, 2016 7:01 am 
Beginner
Beginner

Joined: Wed Jul 30, 2008 8:43 am
Posts: 32
@anshuiitk
Thanks man, you article helped a lot. I would day current hibernate implementation is really strange - for insert listener changing of the entity affects the SQL insert, but for update listener - not.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.