-->
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.  [ 5 posts ] 
Author Message
 Post subject: Updating persistent property in PreInsertEventListener
PostPosted: Wed Mar 30, 2005 10:41 am 
Regular
Regular

Joined: Tue Sep 28, 2004 5:18 pm
Posts: 55
Location: Switzerland
Hibernate version: 3.0

Name and version of the database you are using: FirstSQL 3.0


I'm experimenting with the PreInsertEventListener.
Although the code below gets executed correctly and the dataHash property is properly updated, the updated value is not written to disk, and the dataHash property remains null in the db record corresponding to the Thingy.

Should it be possible to modify entity values prior to insert/update? If so, why might the updated dataHash property fail to be written?



Code:
public class SBPreInsertListener extends DefaultPreInsertEventListener {

    public SBPreInsertListener() {
    }

    public boolean onPreInsert(PreInsertEvent event) {
        if (event.getEntity() instanceof Thingy) {
            final Thingy thingy = (Thingy) event.getEntity();
            thingy.setDataHash(thingy.getThingyData().save());
        }
        return super.onPreInsert(event);
    }
}


Top
 Profile  
 
 Post subject: solved
PostPosted: Wed Mar 30, 2005 11:28 am 
Regular
Regular

Joined: Tue Sep 28, 2004 5:18 pm
Posts: 55
Location: Switzerland
Code:
public class SBSaveOrUpdateListener extends DefaultSaveOrUpdateEventListener {

    public SBSaveOrUpdateListener() {
    }

    public Serializable onSaveOrUpdate(SaveOrUpdateEvent event) throws HibernateException {
        if (event.getObject() instanceof Thingy) {
            final Thingy thingy = (Thingy) event.getObject();
            thingy.setDataHash(thingy.getThingyData().save());
        }
        return super.onSaveOrUpdate(event);
    }

}


Top
 Profile  
 
 Post subject: PreInsertEventListener buggy confirmation
PostPosted: Fri Jan 25, 2008 2:51 am 
Newbie

Joined: Wed Jan 23, 2008 7:45 am
Posts: 14
Location: Pune,India
Even I have experimented with PreInsertListener and found that the changes on the entity do not persist. Please avoid using this.

_________________
Harshal Vaidya


Top
 Profile  
 
 Post subject: gotcha~~
PostPosted: Tue Apr 15, 2008 5:07 am 
Newbie

Joined: Mon Jan 15, 2007 4:05 am
Posts: 2
Inspired by org.hibernate.EmptyInterceptor!

Code:
public boolean onPreInsert(PreInsertEvent event) {
   event.getState()[ArrayUtils.indexOf(event.getPersister().getPropertyNames(), "createDate")] = new Date();
   return false;
}


Top
 Profile  
 
 Post subject: Re: Updating persistent property in PreInsertEventListener
PostPosted: Wed Mar 03, 2010 9:12 am 
Newbie

Joined: Wed Mar 11, 2009 5:13 pm
Posts: 4
ok,
thank you. your solution is working...
but why?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.