-->
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: Event handling: lastModification tracing
PostPosted: Mon Jun 20, 2005 8:48 am 
Newbie

Joined: Fri Jun 17, 2005 2:51 am
Posts: 11
Hi,

I want to record the last modification of a data record by using the event handling system. I wrote my own SaveOrUpdateEvent like this:

Code:
public class MySaveOrUpdateEventListener extends DefaultSaveOrUpdateEventListener {
   
   public Serializable onSaveOrUpdate(SaveOrUpdateEvent event) {
      if (event.getObject() instanceof IAuditedObject) {
         IAuditedObject audObj = (IAuditedObject)event.getObject();
         audObj.setLastModification(new Date());
      }
      return super.onSaveOrUpdate(event);
   }
   
}


and registered this event like this in the hibernate.cfg.xml:

Code:
    <listener type="save" class="MySaveOrUpdateEventListener"/>
    <listener type="update" class="MySaveOrUpdateEventListener"/>
    <listener type="save-update" class="MySaveOrUpdateEventListener"/>

As this woks fine when creating new records, the event handler is not called when updating an existing record like this:

Code:
MyObject myObj = HibernateUtil.getSession().load(MyObject.class, myIdId);
myObj.setAttr("some value");

Most likely the event handler is not called because I don't save the changed object explicitely on the session (session.saveOrUpdate(myObj)).
Which event handler will be called in this case? Do I have to extend more than one event handler or does there exist a event handler that will be called before every DB writing?

Thanks in advance,
Joern


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 20, 2005 11:04 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
See the Inteceptor usage.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 20, 2005 11:08 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
http://www.hibernate.org/hib_docs/v3/re ... le/#events


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 21, 2005 2:56 am 
Newbie

Joined: Fri Jun 17, 2005 2:51 am
Posts: 11
Hi alesj,

I know that I can write the last modification date using interceptors. However, I like the event handling system because you can configure it in the hibernate.cfg.xml file, so I was looking for a solution based on the event system.
Most likely I have to play around with the FlushEntityEventListner to write the last modification date on every entity change.
By the way, instead of writing the last modification date I want to create a change entry in another table and reference this change from all changed entites within the transaction. And from what I know by reading this forum is creating new entities from within the interceptor somewhat tricky. But I keep on playing around....

Best regards,
Joern


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 06, 2005 4:24 am 
Expert
Expert

Joined: Thu Sep 04, 2003 8:23 am
Posts: 368
In your case, the PostUpdateEventListener is called, so you can put your code in such an event listener.

Seb


Top
 Profile  
 
 Post subject: PreUpdate/SaveOrUpdate event listeners problem
PostPosted: Sat Mar 25, 2006 11:53 pm 
Newbie

Joined: Mon Mar 21, 2005 12:27 am
Posts: 17
I have the same confusions over these listeners.

The SaveOrUpdateEventListener is not invoked during update of an entity.

The PreUpdateEventListener is invoked for entity update after the updated fields are collected by Hibernate, so that any changes made to the updating entity are never written to database.

_________________
Hacking Bear


Top
 Profile  
 
 Post subject:
PostPosted: Sun Mar 26, 2006 12:31 am 
Expert
Expert

Joined: Mon Jan 09, 2006 5:01 pm
Posts: 311
Location: Sacramento, CA
what is the complete mapping (hbm.xml) for the class "MyObject"?

_________________
-JT

If you find my replies helpful, please rate by clicking 'Y' on them. I appreciate it.


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.