title: nHibernate mysteriously removing items from many-to-one relationship
body: <p>I have a simple application where users can open orders, make changes to them, then save them back to the database.  When an order is opened, I retrieve it from the database like this</p>
  
  <pre><code>ISession session = SessionFactory.OpenSession();
  
  ...
  
  Order order = session.Query&lt;Order&gt;()
                       .Where(o =&gt; o.Id == id)
                       .FirstOrDefault();
  </code></pre>
  
  <p>To enable lazy loading of order.Comments, I keep the session open until the order is closed.  Here are the mappings:</p>
  
  <pre><code>&lt;class name="Order"&gt;
      &lt;id name="Id"&gt;
          &lt;generator class="identity" /&gt;
      &lt;/id&gt;    
  
  ...
  
      &lt;set name="Comments" access="field.camelcase-underscore" cascade="all-delete-orphan"    lazy="true" order-by="Created"&gt;
        &lt;key column="OrderId" /&gt;
        &lt;one-to-many class="Comment" /&gt;
      &lt;/set&gt;     
  &lt;/class&gt;
  
  &lt;class name="Comment" table="OrderComment" lazy="false"&gt;
      &lt;id name="Id"&gt;
        &lt;generator class="identity" /&gt;
      &lt;/id&gt;
      &lt;many-to-one name="Author" /&gt;
      &lt;property name="Created" /&gt;
      &lt;property name="Text" length="1000" /&gt;
  &lt;/class&gt;
  </code></pre>
  
  <p>The app is designed so that while an order is open, it can be saved several times before it is closed.  I save like this:</p>
  
  <pre><code>using (ITransaction trans = session.BeginTransaction())
  {
      session.SaveOrUpdate(order)
      trans.Commit();
  }
  </code></pre>
  
  <p>Finally, when the user closes the order, I dispose of the session.</p>
  
  <p>Here's there problem: If the user adds a comment, saves, and then before closing the order, adds another comment and saves again, the first comment is removed during the second save. Here's the sql that is outputted from the second save:</p>
  
  <pre><code>NHibernate: INSERT INTO OrderComment (Id, Author, Created, Text) VALUES (hibernate_sequence.nextval, :p0, :p1, :p2) returning Id into :nhIdOutParam;:p0 = 1 [Type: Int32 (0)], :p1 = 14.01.2013 12:53:20 [Type: DateTime (0)], :p2 = '2' [Type: String (0)], :nhIdOutParam = NULL [Type: Int32 (0)]
  
  **NHibernate: UPDATE OrderComment SET OrderId = null WHERE OrderId = :p0 AND Id = :p1;:p0 = 465 [Type: Int32 (0)], :p1 = 591 [Type: Int32 (0)]**
  
  NHibernate: UPDATE OrderComment SET OrderId = :p0 WHERE Id = :p1;:p0 = 465 [Type: Int32 (0)], :p1 = 592 [Type: Int32 (0)]
  </code></pre>
  
  <p>So the problem is that line in bold - the OrderId for the first comment is being set to null.  Can anybody tell my why?</p>
  
  <p>Is there anything wrong with the way I'm using nHibernate here?  To reiterate what I do:</p>
  
  <ol>
  <li>Open a session</li>
  <li>Retrieve an object</li>
  <li>User updates the object</li>
  <li>Save the object by beginning a transaction, calling session.SaveOrUpdate(object), then committing the transaction.</li>
  <li>Repeat steps 3 and 4 any number of times.</li>
  <li>Dispose of the session.</li>
  </ol>
  
  <p>Is this an acceptable way to use <code>nHibernate</code>? </p>
  
  <p><strong>Edit</strong></p>
  
  <p>Here is the comments property in the Order class:</p>
  
  <pre><code>ICollection&lt;Comment&gt; _comments = new List&lt;Comment&gt;();
  public virtual ReadOnlyCollection&lt;Comment&gt; Comments
  {
      get 
      {
          return _comments.ToList().AsReadOnly(); 
      }
  }
  </code></pre>
  
  <p>Then comments are added by calling the following method:</p>
  
  <pre><code>public virtual void AddComment(Comment comment)
  {
      _comments.Add(comment);    
  }
  
   ...
  
  Comment comment = new Comment()
  {
      Author = User.Current,
      Created = DateTime.Now,
      Text = text
  };
  
  order.AddComment(comment);
  </code></pre>
  
  <p>Here is the Comment class. Id is implemented in the base class <code>PersistentObject&lt;T&gt;</code>:</p>
  
  <pre><code>public class Comment : PersistentObject&lt;int&gt;
  {        
      public User Author { get; private set; }
      public DateTime Created { get; private set; }
      public string Text { get; private set; }                                             
  }
  
  public abstract class PersistentObject&lt;T&gt;
  {        
      public virtual T Id { get; protected internal set; }       
  
      public override bool Equals(object obj)
      {
          // If both objects have not been saved to database, then can't compare Id because this
          // will be 0 for both.  In this case use reference equality.
  
          PersistentObject&lt;T&gt; other = obj as PersistentObject&lt;T&gt;;
          if (other == null)
              return false;
  
          bool thisIsDefault = object.Equals(Id, default(T));
          bool otherIsDefault = object.Equals(other.Id, default(T));
  
          if (thisIsDefault &amp;&amp; otherIsDefault)
              return object.ReferenceEquals(this, other);
          else if (thisIsDefault || otherIsDefault)
              return false;
          else
              return object.Equals(this.Id, other.Id);
      }
  
      public override int GetHashCode()
      {
          return Id.GetHashCode();
      }        
  }
  </code></pre>