title: Add objects to association in OnPreInsert, OnPreUpdate
body: <p>I have an event listener (for Audit Logs) which needs to append audit log entries to the association of the object:</p>
  
  <pre><code>public Company : IAuditable {
      // Other stuff removed for bravety
      IAuditLog IAuditable.CreateEntry() {
          var entry = new CompanyAudit();
          this.auditLogs.Add(entry);
          return entry;
      }
      public virtual IEnumerable&lt;CompanyAudit&gt; AuditLogs {
          get { return this.auditLogs }
      }
  }
  </code></pre>
  
  <p>The <code>AuditLogs</code> collection is mapped with <strong>cascading</strong>:</p>
  
  <pre><code>public class CompanyMap : ClassMap&lt;Company&gt; {
      public CompanyMap() {
          // Id and others removed fro bravety
          HasMany(x =&gt; x.AuditLogs).AsSet()
              .LazyLoad()
              .Access.ReadOnlyPropertyThroughCamelCaseField()
              .Cascade.All();
      }
  }
  </code></pre>
  
  <p>And the listener just asks the auditable object to create log entries so it can update them:</p>
  
  <pre><code>internal class AuditEventListener : IPreInsertEventListener, IPreUpdateEventListener {
      public bool OnPreUpdate(PreUpdateEvent ev) {
          var audit = ev.Entity as IAuditable;
          if (audit == null)
              return false;
          Log(audit);
          return false;
      }
  
  
      public bool OnPreInsert(PreInsertEvent ev) {
          var audit = ev.Entity as IAuditable;
          if (audit == null)
              return false;
  
          Log(audit);
          return false;
      }
      private static void Log(IAuditable auditable) {
          var entry = auditable.CreateAuditEntry();  // Doing this for every auditable property
          entry.CreatedAt = DateTime.Now;
          entry.Who = GetCurrentUser(); // Might potentially execute a query as it links current user with log entry
          // Also other information is set for entry here
      }
  }
  </code></pre>
  
  <p>The problem with it though is that it throws <code>TransientObjectException</code> when commiting the transaction:</p>
  
  <pre><code>NHibernate.TransientObjectException : object references an unsaved transient instance - save the transient instance before flushing. Type: CompanyAudit, Entity: CompanyAudit
      at NHibernate.Engine.ForeignKeys.GetEntityIdentifierIfNotUnsaved(String entityName, Object entity, ISessionImplementor session)
      at NHibernate.Type.EntityType.GetIdentifier(Object value, ISessionImplementor session)
      at NHibernate.Type.ManyToOneType.NullSafeSet(IDbCommand st, Object value, Int32 index, Boolean[] settable, ISessionImplementor session)
      at NHibernate.Persister.Collection.AbstractCollectionPersister.WriteElement(IDbCommand st, Object elt, Int32 i, ISessionImplementor session)
      at NHibernate.Persister.Collection.AbstractCollectionPersister.PerformInsert(Object ownerId, IPersistentCollection collection, IExpectation expectation, Object entry, Int32 index, Boolean useBatch, Boolean callable, ISessionImplementor session)
      at NHibernate.Persister.Collection.AbstractCollectionPersister.Recreate(IPersistentCollection collection, Object id, ISessionImplementor session)
      at NHibernate.Action.CollectionRecreateAction.Execute()
      at NHibernate.Engine.ActionQueue.Execute(IExecutable executable)
      at NHibernate.Engine.ActionQueue.ExecuteActions(IList list)
      at NHibernate.Engine.ActionQueue.ExecuteActions()
      at NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session)
      at NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event)
      at NHibernate.Impl.SessionImpl.Flush()
      at NHibernate.Transaction.AdoTransaction.Commit()
  </code></pre>
  
  <p>As the <strong>cascading is set to All</strong> I expected NH to handle this. I also tried to modify the collection using <code>state</code> but pretty much the same happens.</p>
  
  <p>So the question is <strong>what is the last chance to modify object's associations before it gets saved</strong>?</p>
  
  <p>Thanks,<br>
  Dmitriy.</p>