Why are you using IPostInsertEventListener, AFAIK this listener is fired after the entity is saved to db, it seems like you need to do the correction before the entity is saved.
maybe this might help http://ayende.com/blog/3987/nhibernate-ipreupdateeventlistener-ipreinserteventlistener On Tue, Mar 10, 2015 at 1:00 AM, JJ BB <[email protected]> wrote: > Jeffrey, > > thank you for your suggestion, but I don't wish to use envers. I would > like to use IPostInsertEventListener: it gives me full control of this > process. > > Cheers > > > El dilluns, 9 març de 2015 20:25:54 UTC+1, Jeffrey Becker va escriure: > >> NHibernate can the handle versioning for you with the version property >> <http://nhibernate.info/doc/nh/en/#mapping-declaration-version>. >> NHibernate.Envers <https://bitbucket.org/RogerKratz/nhibernate.envers> >> is probably what you want for auditing. >> >> On Monday, March 9, 2015 at 2:02:05 PM UTC-4, JJ BB wrote: >>> >>> Hi folks, >>> >>> I'm into a record versioning requirement of all mapped entities (using >>> hbm.xml files). >>> >>> public class User >>> { >>> public virtual String UserName { get; set; } >>> public virtual String Password { get; set; } >>> (...) >>> public virtual Int32 SysVersion { get; set; } >>> } >>> >>> I'm invoking IPostInsertEventListener from nhibernate.cfg.xml: >>> >>> <event type="post-insert"> >>> <listener class="myEvents.PostInsert, myAssembly /> >>> </event> >>> >>> This is the code: >>> >>> public class PostInsert : IPostInsertEventListener >>> { >>> //IPreInsertEventListener >>> //public bool OnPreInsert(PreInsertEvent preInsertEvent) >>> //{ >>> // Correction(preInsertEvent.Entity); >>> // return false; >>> //} >>> >>> //internal static void Correction(object entity) >>> //{ >>> // var sysVersion = entity.GetType(). >>> GetProperties().FirstOrDefault >>> // ( >>> // p => p.PropertyType == typeof(Int32) && >>> p.Name.ToLower() == "sysversion" >>> // ); >>> >>> // if (sysVersion == null) >>> // return; >>> >>> // sysVersion.SetValue(entity, 1, null); >>> //} >>> public void OnPostInsert(PostInsertEvent @event) >>> { >>> //@event.Entity >>> >>> var sysVersion = @event.Entity.GetType(). >>> GetProperties().FirstOrDefault >>> ( >>> p => p.PropertyType == typeof(Int32) && >>> p.Name.ToLower() == "sysversion" >>> ); >>> >>> if (sysVersion == null) >>> return; >>> >>> sysVersion.SetValue(@event.Entity, 1, null); >>> } >>> >>> I need centralize sysVersion Insert and Update and then audit both >>> operations in an audit table. Code runs sysVersion.SetValue, but, NH does >>> not save sysVersion=1 into the table. >>> >>> Please can you help me ? >>> >> -- > You received this message because you are subscribed to the Google Groups > "nhusers" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/nhusers. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "nhusers" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/nhusers. For more options, visit https://groups.google.com/d/optout.
