title: NHibernate : unexpected session trouble when persisting object
body: <p>I'm having trouble with some NHibernate Code.Basically I'm trying to keep session lifetime as short as possible to minimize state information in the application.</p>
  
  <p>I find it rather hard to describe my problem without being specific, so I'll just go ahead and use Article and Category metaphors.</p>
  
  <pre><code>public class Article
  {
      public virtual long Id { get; set; }
      public virtual string Name { get; set; }
      public virtual Category Category { get; set; }
  }
  public class Category
  {
      public virtual long Id { get; set; }
      public virtual string Name { get; set; }
      public override string ToString() { return "cat-" + this.Id.ToString(); }
  }
  public class ArticleMapping : ClassMap&lt;Article&gt;
  {
      public ArticleMapping()
      {
          this.Id( x =&gt; x.Id, "id" ).GeneratedBy.Assigned();
          this.Map( x =&gt; x.Name, "name" );
          this.References( x =&gt; x.Category ).Fetch.Join().Cascade.None();
      }
  }
  public class CategoryMapping : ClassMap&lt;Category&gt;
  {
      public CategoryMapping()
      {
          this.ReadOnly();
          this.Id(x =&gt; x.Id, "id").GeneratedBy.Assigned();
          this.Map(x =&gt; x.Name, "name");
      }
  }
  </code></pre>
  
  <p>What I do is create a new <strong>Article</strong>, give it a name, Assign a <strong>Category</strong> from a list loaded via the method described below and attempt to save.</p>
  
  <p>I get the following warning:</p>
  
  <blockquote>
    <p>Unable to determine if cat-1 with assigned identifier is transient or detached</p>
  </blockquote>
  
  <p>If you take a look at the model and mapping code, you will see that cat-1 should not be persisted at all - it is a Category and defined readonly and non-cascading.</p>
  
  <pre><code>//code that loads the list of categories
  IStatelessSession session = this.SessionService.GetStatelessSession();
  IList&lt;Category&gt; cats = session.CreateCriteria&lt;Category&gt;().List&lt;Category&gt;();
  this.SessionService.EndSession(session);
  
  //code that's called to save the instance
  ISession session = this.SessionService.GetSession();
  using (ITransaction transaction = session.BeginTransaction())
  {
    session.SaveOrUpdate(article);
    transaction.Commit();
  }
  this.SessionService.EndSession(session);
  </code></pre>
  
  <p>If I call the Save method again, it all goes banana-shaped: </p>
  
  <p><strong>Exception:</strong> </p>
  
  <blockquote>
    <p>NHibernate.StaleStateException: Unexpected row count: 0; expected: 1</p>
  </blockquote>
  
  <p><strong>Stacktrace:</strong> </p>
  
  <p>at NHibernate.AdoNet.Expectations.BasicExpectation.VerifyOutcomeNonBatched(Int32 rowCount, IDbCommand statement)
  at NHibernate.AdoNet.NonBatchingBatcher.AddToBatch(IExpectation expectation)</p>
  
  <p>at NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object id, Object[] fields, 
  Object[] oldFields, Object rowId, Boolean[] includeProperty, Int32 j, Object oldVersion, Object obj, SqlCommandInfo sql, ISessionImplementor session)</p>
  
  <p>at NHibernate.Persister.Entity.AbstractEntityPersister.UpdateOrInsert(Object id, Object[] fields, Object[] oldFields, Object rowId, Boolean[] includeProperty, Int32 j, Object oldVersion, Object obj, SqlCommandInfo sql, ISessionImplementor session)</p>
  
  <p>at NHibernate.Persister.Entity.AbstractEntityPersister.Update(Object id, Object[] fields, Int32[] dirtyFieldA first chance exception of type 'NHibernate.StaleStateException' occurred in NHibernate.dll
  s, Boolean hasDirtyCollection, Object[] oldFields, Object oldVersion, Object obj, Object rowId, ISessionImplementor session)</p>
  
  <p>at NHibernate.Action.EntityUpdateAction.Execute()</p>
  
  <p>at NHibernate.Engine.ActionQueue.Execute(IExecutable executable)</p>
  
  <p>at NHibernate.Engine.ActionQueue.ExecuteActions(IList list)</p>
  
  <p>at NHibernate.Engine.ActionQueue.ExecuteActions()</p>
  
  <p>at NHibernate.Event.Default.AbstractFlushingEventListener.PerformExecutions(IEventSource session)</p>
  
  <p>I used to think NHibernate was free from black magic, but this makes me wanna visit my voodoo priest to ask for help from the other realm...</p>
  
  <p>Does anyone have any idea where the StaleStateException is coming from? Where did I go wrong this time around?</p>
  
  <p>Thanks in advance </p>
  
  <p>Sebi</p>