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

  • Home
  • SEARCH
  • 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 4549252
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T16:20:04+00:00 2026-05-21T16:20:04+00:00

Overview : With NHibernate I am experimenting with a 3 layered hierarchy using joined

  • 0

Overview: With NHibernate I am experimenting with a 3 layered hierarchy using joined subclasses. There is a Category, which inherits from AuditableEntity (to add PreUpdate and PreInsert audit trail), which finally inherits from an Entity.

Problem: None of the data changes to the AuditableEntity object, which are carried out exactly as Ayende’s blog post, are being persisted to the database. The AuditableEntity objects properties are successfully updated by the PreUpdate code, but it is as if NHibernate is not seeing the AuditableEntity as dirty as no update sql statement occurs.

Hbm:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="Learning"
                   namespace="Learning.entities">
  <class name="Entity" >
    <id name="Id" type="guid">
      <generator class="guid.comb"></generator>
    </id>
    <version name="Version"/>

    <joined-subclass name="AuditableEntity" >
      <key column="AuditableEntity_id"></key>

      <property name="CreatedOn" ></property>
      <property name="CreatedBy" ></property>
      <property name="LastModifiedOn" ></property>
      <property name="LastModifiedBy" ></property>

      <joined-subclass name="Category">
        <key column="AuditableEntity_id"></key>
        <property name="Name" />
      </joined-subclass>

    </joined-subclass>    
  </class>
</hibernate-mapping>

NHibernate config for listeners:

<event type="pre-insert">
  <listener class="Learning.eventlisteners.AuditInsertEventListener, Learning" />
</event>
<event type="pre-update">
  <listener class="Learning.eventlisteners.AuditUpdateEventListener, Learning" />
</event>

PreUpdate code:

namespace Learning.eventlisteners
{
    public class AuditInsertEventListener : IPreInsertEventListener
    {
        public bool OnPreInsert(PreInsertEvent @event)
        {
            var audit = @event.Entity as IAuditable;
            if (audit == null)
                return false;

            var createdOn = DateTime.Now;
            var createdBy = loggedOnProfile;

            AuditCommon.Set(@event.Persister, @event.State, "CreatedOn", createdOn);
            AuditCommon.Set(@event.Persister, @event.State, "CreatedBy", createdBy);
            AuditCommon.Set(@event.Persister, @event.State, "LastModifiedOn", createdOn);
            AuditCommon.Set(@event.Persister, @event.State, "LastModifiedBy", createdBy);

            audit.CreatedOn = createdOn;
            audit.CreatedBy = createdBy;
            audit.LastModifiedOn = createdOn;
            audit.LastModifiedBy = createdBy;

            return false;
        }
    }

    public static class AuditCommon
    {
        internal static void Set(IEntityPersister persister, IList<object> state, string propertyName, object value)
        {
            var index = Array.IndexOf(persister.PropertyNames, propertyName);
            if (index == -1)
                return;
            state[index] = value;
        }
    }

    public class AuditUpdateEventListener : IPreUpdateEventListener
    {
        public bool OnPreUpdate(PreUpdateEvent @event)
        {
            var audit = @event.Entity as IAuditable;
            if (audit == null)
                return false;

            var lastModifiedOn = DateTime.Now.AddSeconds(28);
            var lastModifiedBy = loggedOnProfile;

            AuditCommon.Set(@event.Persister, @event.State, "LastModifiedOn", lastModifiedOn);
            AuditCommon.Set(@event.Persister, @event.State, "LastModifiedBy", lastModifiedBy);

            audit.LastModifiedOn = lastModifiedOn;
            audit.LastModifiedBy = lastModifiedBy;

            return false;
        }
    }
}

Code:

using (var session = SessionFactory.OpenSession())
using (var transaction = session.BeginTransaction())
{
    var category = session.Query<Category>().First();
    category.Name = "Updated";
    session.SaveOrUpdate(category);
    transaction.Commit();
}

An observation: if I manually update just one of the AuditableEntity properties before calling SaveOrUpdate, the PreUpdate event is obviously fired and appropriate data changes are made, and then the AuditableEntity data IS persisted to the database.

using (var session = SessionFactory.OpenSession())
using (var transaction = session.BeginTransaction())
{
    var category = session.Query<Category>().First();
    category.Name = "Updated";
    category.CreatedOn = DateTime.Now;
    session.SaveOrUpdate(category);
    transaction.Commit();
}

Help: I obviously don’t want to have to dummy edit an AuditableEntity properties, so any ideas as to what I am doing wrong here?

  • 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-21T16:20:05+00:00Added an answer on May 21, 2026 at 4:20 pm

    To answer this I have authored an nhibernate.info WIKI article – http://nhibernate.info/doc/howto/various/changing-values-in-nhibernate-events

    The abstract; Audit trails using NHibernate’s event model often use the OnPreInsert and OnPreUpdate event listeners to change/ modify the state of the entity. While this does works and is widely documented as a solution, it should be noted the OnPreInsert and OnPreUpdate events are not intended to be used to change the values of the entity and instead they should be used to check values and for that reason they return “veto”.

    An update blog post from Fabio – http://fabiomaulo.blogspot.com/2011/05/nhibernate-bizarre-audit.html

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

Sidebar

Related Questions

Is there any good ICriteria API overview? Chapter 12 from the official NHibernate reference
There seem to be very different opinions about using transactions for reading from a
Is there an exact overview what has changed in the SP1 for .NET 3.5?
Is there any way to see an overview of what kind of queries are
Overview: I have an array of 20 byte strings that needs to be stored
What's the widest overview and where are the deepest analysis of different replication methods
How to change overview rule background color in Eclipse 3.4.0.I20080617-2000 (vertical bar on right
I want to get an overview of files that are updated in TFS (that
Could somebody give me a brief overview of the differences between HTTP 1.0 and
This article gives a good overview on why structured exception handling is bad. Is

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.