-->
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: Object update by interceptor's onSave() causes extra UPDATE
PostPosted: Wed Feb 02, 2005 1:04 pm 
Newbie

Joined: Tue Nov 23, 2004 8:19 am
Posts: 12
Location: Santa Maria-RS, Brazil
Hi,

I'm using an interceptor to update some auditing properties of the entities when the entity is saved/updated. I expected hibernate to execute only one SQL statement (INSERT), however it executes two (one INSERT and one UPDATE). This behavior occurs in both onSave() and onFlushDirty() methods.

Is this the correct behavior? I know that entity updates after a call to Session.save() generate UPDATE statements, but is the entity state already "saved" when onSave() is called?

The interceptor code:

Code:
    public boolean onFlushDirty(Object entity, Serializable id,
                                Object[] currentState, Object[] previousState,
                                String[] propertyNames, Type[] types)
            throws CallbackException {
        if (entity instanceof SimVersionedEntity) {
            updateSimEntity(propertyNames, currentState);
            return true;
        } else
            return false;
    }

    public boolean onSave(Object entity, Serializable id, Object[] state,
                          String[] propertyNames, Type[] types)
            throws CallbackException {
        if (entity instanceof SimVersionedEntity) {
            updateSimEntity(propertyNames, state);
            return true;
        } else
            return false;
    }

    private void updateSimEntity(String[] propertyNames,
                                  Object[] currentState) {
        final Date now = new Date();
        for (int i = 0; i < propertyNames.length; i++) {
            if ("updtDate".equals(propertyNames[i])
                    || "updtTime".equals(propertyNames[i])) {
                currentState[i] = now;
            } else if ("userCode".equals(propertyNames[i])) {
                currentState[i] = new Long(1);
            }
        }
    }


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 02, 2005 8:15 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
changing the property values in onSave() should not cause an extra update, no.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 03, 2005 8:07 am 
Newbie

Joined: Tue Nov 23, 2004 8:19 am
Posts: 12
Location: Santa Maria-RS, Brazil
thx, gavin

Indeed, I thought I was right, because the onSave() documentation says "Called before an object is saved". So, I guess it's a bug. By the way, I'm using the beta3 version of Hibernate.


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:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.