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 213715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:18:18+00:00 2026-05-11T18:18:18+00:00

I am using NHibernate interceptors to log information about Updates/Inserts/Deletes to my various entities.

  • 0

I am using NHibernate interceptors to log information about Updates/Inserts/Deletes to my various entities.

Included in the information logged is the Entity Type and the Unique Id of the entity modified. The unique Id is marked as a <generator class="identity"> in the NHibernate mapping file.

The obvious problem is when logging an Insert operation using IInterceptor.OnSave() the Id of the entity has not yet been assigned.

How can I obtain the Id of the inserted entity before logging the audit information?

(I have looked into NHibernate Listeners PostSave event but can’t get them working with the Spring.net configuration being used, so I would like to stick with interceptors if at all possible)

Code:

    // object id parameter is null...
    public override bool OnSave(object entity, object id, object[] state, 
        string[] propertyNames, IType[] types)
    {            
        AddAuditItem(entity, INSERT);
        return false;            
    }
  • 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-11T18:18:18+00:00Added an answer on May 11, 2026 at 6:18 pm

    I’ve worked around this problem by adding a list to my interceptor class which is populated with objects during the OnSave implementation.

    In the PostFlush implementation the list is iterated over and each element is audited as an insert. The objects in this list have been persisted in PostFlush() and thus have generated IDs.

    This seems to work OK but I’d be grateful if any potential pitfalls were pointed out 🙂

    public class AuditInterceptor : EmptyInterceptor
    {       
        // To hold inserted items to be audited after insert has been flushed
        private IList<object> insertItems = new List<object>();
    
        public override void PostFlush(System.Collections.ICollection entities)
        {            
            foreach (var entity in insertItems)
            {
                AddAuditItem(entity, INSERT);
            }
            insertItems.Clear();
    
            base.PostFlush(entities);
        }
    
        public override bool OnSave(object entity, object id, object[] state, 
            string[] propertyNames, IType[] types)
        {            
            var auditable = entity as IAuditable;
            if (auditable != null) 
                insertItems.Add(entity);
    
            return false;            
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using nhibernate to query my db and am seeing various statements in
I am using NHibernate and log4net. This is a snapshot of my error log
I am using NHibernate and need to update/add an entity class(a) that can reference
I'm using NHibernate to map some very simple entities to three tables. My program
I'm using Fluent NHibernate to generate a database schema from .Net entity classes. I
Using NHibernate 1.x we encountered this error, suddently, during a nigthly job: 25/06/2012 22:50:59
Using NHibernate, is it possible to fill an existing object using the results of
Using NHibernate, what is the best way to handle InsertDate (a.k.a CreateDate) and UpdateDate
I am using NHibernate and switched from my local SQLExpress database to Oracle11g. My
I'm using NHibernate on a legacy database with Oracle 8i client. I can perform

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.