Note that there are some explanatory texts on larger screens.

plurals
  1. PONHibernate, joined subclass hierarchy, PreUpdate event data changes on an entity which is only modified in the PreUpdate event is not persisted
    primarykey
    data
    text
    <p><strong>Overview</strong>: 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.</p> <p><strong>Problem</strong>: 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. </p> <p><strong>Hbm</strong>: </p> <pre><code>&lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Learning" namespace="Learning.entities"&gt; &lt;class name="Entity" &gt; &lt;id name="Id" type="guid"&gt; &lt;generator class="guid.comb"&gt;&lt;/generator&gt; &lt;/id&gt; &lt;version name="Version"/&gt; &lt;joined-subclass name="AuditableEntity" &gt; &lt;key column="AuditableEntity_id"&gt;&lt;/key&gt; &lt;property name="CreatedOn" &gt;&lt;/property&gt; &lt;property name="CreatedBy" &gt;&lt;/property&gt; &lt;property name="LastModifiedOn" &gt;&lt;/property&gt; &lt;property name="LastModifiedBy" &gt;&lt;/property&gt; &lt;joined-subclass name="Category"&gt; &lt;key column="AuditableEntity_id"&gt;&lt;/key&gt; &lt;property name="Name" /&gt; &lt;/joined-subclass&gt; &lt;/joined-subclass&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p><strong>NHibernate config for listeners</strong>:</p> <pre><code>&lt;event type="pre-insert"&gt; &lt;listener class="Learning.eventlisteners.AuditInsertEventListener, Learning" /&gt; &lt;/event&gt; &lt;event type="pre-update"&gt; &lt;listener class="Learning.eventlisteners.AuditUpdateEventListener, Learning" /&gt; &lt;/event&gt; </code></pre> <p><strong>PreUpdate code</strong>:</p> <pre><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&lt;object&gt; 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></pre> <p><strong>Code</strong>:</p> <pre><code>using (var session = SessionFactory.OpenSession()) using (var transaction = session.BeginTransaction()) { var category = session.Query&lt;Category&gt;().First(); category.Name = "Updated"; session.SaveOrUpdate(category); transaction.Commit(); } </code></pre> <p><strong>An observation</strong>: 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.</p> <pre><code>using (var session = SessionFactory.OpenSession()) using (var transaction = session.BeginTransaction()) { var category = session.Query&lt;Category&gt;().First(); category.Name = "Updated"; category.CreatedOn = DateTime.Now; session.SaveOrUpdate(category); transaction.Commit(); } </code></pre> <p><strong>Help</strong>: I obviously don't want to have to dummy edit an AuditableEntity properties, so any ideas as to what I am doing wrong here?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload