Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7430063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:04:47+00:00 2026-05-29T09:04:47+00:00

After looking over the of the NHibernate.Envers code I realized I was implementing the

  • 0

After looking over the of the NHibernate.Envers code I realized I was implementing the wrong interface. Now that I know what interface(s) to use things are working a little better.

My current implementation looks like this:

public class PreCollectionUpdate : IPreCollectionUpdateEventListener
{
    public void OnPreUpdateCollection(PreCollectionUpdateEvent @event)
    {
        var collectionEntry = @event.Session.PersistenceContext.GetCollectionEntry(@event.Collection);

        if(!collectionEntry.LoadedPersister.IsInverse)
            return;

        var collection = @event.Collection;
        var collectionEntries = collection.Entries(collectionEntry.LoadedPersister);

        foreach(var entry in collectionEntries)
        {
            if(!(entry is TrackableEntity))
                return;

            var trackableEntity = entry as TrackableEntity;
            trackableEntity.AddedAt = Time.Now;
            trackableEntity.AddedBy = User.Current;
        }
    }
}

Through debugging I can see it is being called and properly modifying my collection. For some reason however, it first inserts the items in my collection with default values for AddedAt and AddedBy then later performs an update to fill said values.

Here is my test code:

using (var transaction = Session.BeginTransaction())
{
  var locate = new Locate
                   {
                       TicketNumber = 123456789,
                       Status = Status.InProgress
                   };
  Session.Save(locate);
  transaction.Commit();
}

using (var transaction = Session.BeginTransaction())
{
  var locate1 = Session.Get<Locate>(1);
  locate1.AddReview(new AllClear());
  Session.Save(locate1);
  transaction.Commit();
}

using (var transaction = Session.BeginTransaction())
{
  var locate1 = Session.Load<Locate>(1);
  transaction.Commit();
}

Why is that?

Debugging my test I can see that after I commit my transaction that both AddedBy and AddedWhen properties are properly filled. Simply not sure why it isn’t committing the modified collection.

Adding a bunch of Console.Write statements all over my code I can see that my event listener is being called after I commit my session.

NHibernate: INSERT INTO Locates (AddedAt, AddedBy, TicketNumber, Status, SomeOtherField) VALUES (@p0, @p1, @p2, @p3, @p4); select last_insert_rowid();@p0 = 1/1/0001 00:00:00 [Type: DateTime (0)], @p1 = NULL [Type: String (0)], @p2 = 123456789 [Type: Int64 (0)], @p3 = 'InProgress' [Type: String (0)], @p4 = NULL [Type: String (0)]
Saving review...
Commiting...
NHibernate: INSERT INTO "Review" (AddedAt, AddedBy, Locate_id) VALUES (@p0, @p1, @p2); select last_insert_rowid();@p0 = 1/1/0001 00:00:00 [Type: DateTime (0)], @p1 = NULL [Type: String (0)], @p2 = NULL [Type: Int32 (0)]
In event listener...
Completed filling auditable properties.

I’ve also tried inheritign from IFlushEntityEventListener but the same issue happens. I commit without the AddedAt and AddedBy properties being persisted to the database. My object will however change so next time I work with said object NHibernate will see that it is dirty and issue an update command to the database. What I want is for the AddedAt and AddedBy properties to be committed on the initial commit.

Please let me know if I’m not clear.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:04:48+00:00Added an answer on May 29, 2026 at 9:04 am

    I don’t belive you can use the PreUpdateCollectionEventListener for what you want to do. PreUpdateEventListeners all run very late in the nhibernate pipline Thus at the point that the event has been thrown NHibernate has already seralized the state of the entity to memory. You need to change both the state and the entity itself however in PreUpdateCollectionEvent you do not have access to the state as far as I can tell I have always done what you are trying to do with just the regular PreUpdateEventListener for example something like this should work:

    public class AuditEventListener : IPreUpdateEventListener
    {
        public bool OnPreUpdate(PreUpdateEvent @event)
        {
                // Grab the entity from the event
        var  trackableEntity = @event.Entity as TrackableEntity;
        if ( trackableEntity == null)
            return false;
                // Set the state for the entity  (needed to write new values to the db)
        Set(@event.Persister, @event.State, "AddedAt", time);
        Set(@event.Persister, @event.State, "AddedByBy", name);
    
                // Set the entity values (needed to keep your session in sync)
        trackableEntity.AddedAt = Time.Now;
                trackableEntity.AddedBy = User.Current;
    
        return false;
        }
        private void Set(IEntityPersister persister, object[] state, string propertyName, object value)
    {
        var index = Array.IndexOf(persister.PropertyNames, propertyName);
        if (index == -1)
            return;
        state[index] = value
    }
    

    PS. I know I once read a really good post by Ayende about this but I can’t find it now.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Looking back over my code I find that I occasionaly have written: ResultSet rs
After looking at RescueTime for windows/mac, it seems that there's a version for linux
Hmm, sounds easy enough but after looking at the ones that come with StringTemplate,
PROBLEM SOLVED!! I just found my mistakes, after looking through the debugger over and
After looking at questions like this it doesn't make sense that my __init__(self, parrent,
A friend and I are looking over a website involving a button that makes
From looking over the documentation, it seems that the form clean() method is only
I was looking over some ASP.NET MVC 1 code (C#) in search of the
Yes I know there has been similar posts to this however after looking through
I cannot find this documented anywhere and after looking over Google's api docs for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.