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: Interceptor.OnSave in combination with bags loose changes
PostPosted: Thu Jan 08, 2009 5:35 am 
Newbie

Joined: Fri Apr 25, 2008 8:19 am
Posts: 17
I'm using my own interceptor in the session to set a create/change timestamp. When my PersistentObject Project is updating then i would like to set a changedate, when inserting a createdate. the problem is that my project have a bag. Without bags, sets or lists all works fine! When i using a bag the CreationDate would not save in the database. In debug mode i can see that the value of CreationDate would loose after Saving. The setting of the ChangeDate on an existing object works fine. What can i do?

Code:
public class MyInterceptor : EmptyInterceptor
{             
        public override bool OnSave(object entity, object id, object[] state, string[] propertyNames, NHibernate.Type.IType[] types)
        {           
            if (entity is Project)
            {
                ((PersistentObject)entity).CreationDate = DateTime.Today;
                return true;
            }
            return false; 
        }

        public override bool OnFlushDirty(object entity, object id, object[] currentState, object[] previousState, string[] propertyNames, NHibernate.Type.IType[] types)
        {
            if (entity is Project)
            {
                ((PersistentObject)entity).ChangeDate = DateTime.Today;
                return true;
            }
            return false; 
        }


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2009 8:47 am 
Newbie

Joined: Sat Nov 29, 2008 6:59 pm
Posts: 19
Location: Burlington, VT
You have to set the value on the appropriate entry in the currentState array, you can't set it on the property itself. Setting the value on the array item will also cause it to be set on the property. Here's a sample from my AuditInterceptor:

Code:
        private void SetRevised(object[] currentState, string[] propertyNames)
        {
            int revisedByIndex = Array.FindIndex(propertyNames, s => s.Equals("RevisedBy"));
            int revisedDateIndex = Array.FindIndex(propertyNames, s => s.Equals("RevisedDate"));
            currentState[revisedByIndex] = UserName;
            currentState[revisedDateIndex] = DateTime.Now;
        }

        // Update
        public override bool OnFlushDirty(object entity, object id, object[] currentState, object[] previousState, string[] propertyNames, NHibernate.Type.IType[] types)
        {
            if (entity is IAuditable)
            {
                log.DebugFormat("OnFlushDirty setting revised fields. {0} {1}", entity.GetType().Name, id);
                SetRevised(currentState, propertyNames);
                return true;
            }
            log.DebugFormat("OnFlushDirty called on entity that does not implement IAuditable. {0} {1}", entity.GetType().Name, id);
            return false;
        }


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 08, 2009 10:44 am 
Newbie

Joined: Fri Apr 25, 2008 8:19 am
Posts: 17
Thank you very much! It works fine!


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.