-->
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.  [ 3 posts ] 
Author Message
 Post subject: PreInsertEventListener object modifications not sticky
PostPosted: Thu Mar 31, 2005 1:44 pm 
Newbie

Joined: Fri Mar 11, 2005 6:26 pm
Posts: 2
I have a class that implements PreInsertEventListener.
The onPreInsert() method is called correctly but when I set properties on the object returned from PreInsertEvent.getObject(), they don't "stick" between the time I modify them and the time the insert is called by hibernate.

Here's my class:
public class AuditPreInsertEventListener implements PreInsertEventListener {

public boolean onPreInsert(PreInsertEvent event) throws HibernateException {
if (event.getEntity() instanceof BaseModel) {
BaseModel model = (BaseModel) event.getEntity();
Date now = new Date();
model.setCreatedDate(now);
model.setModifiedDate(now);
}
return false;
}
}

I looked at the source and API doc for PreInsertEventListener, PreInsertEvent as well as the supplied default implementations but there's no commets or documentation. For example, the onPreInsert() method returns true but there's nothing telling me what that value means. Does it mean, don't do the insert if false but do it if true? I have no idea.

Thanks for any help.

Mike


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 19, 2006 8:45 pm 
Newbie

Joined: Wed Apr 19, 2006 8:17 pm
Posts: 1
Just came across the same problem myself and found that you need to change that state values in the event for the insert to work. You still need to change your object too in order to avoid an update occuring later when you flush the session.

I'd say you need something like:

Code:
   public boolean onPreInsert(PreInsertEvent event) {
      if (event.getEntity() instanceof BaseModel) {
         BaseModel model = (BaseModel) event.getEntity();
         Date now = new Date();
         model.setCreatedDate(now);
         model.setModifiedDate(now);
         String[] names = event.getPersister().getPropertyNames();
         Object[] values = event.getState();
         for (int i = 0; i < names.length; i++) {
            if (names[i].equals("createdDate")
                  || names[i].equals("modifiedDate"))
               values[i] = now;
         }
      }
      return false;
   }


Cheers,
Paul.


Top
 Profile  
 
 Post subject: Re: PreInsertEventListener object modifications not sticky
PostPosted: Fri Nov 26, 2010 2:35 am 
Newbie

Joined: Tue May 18, 2010 5:39 am
Posts: 19
you can take a look at http://anshuiitk.blogspot.com/2010/11/h ... event.html. This explains how it works.

_________________
AG
http://anshuiitk.blogspot.com


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.