title: NHibernate + JSON/Ajax Parent/Child Relationships? Why this isn't working?
body: <p>I have a typical <em>parent/child</em> relationship. Writing news in <code>.NET</code> and adding children works fine, <code>NHibernate</code> plays nice and adds the correct relationships.</p>
  
  <p>However, when passing a <code>JSON object</code> from the client to some method that serializes my JSON into the .NET representation, NHibernate seems to get confused. It comes up with the correct query to add the parent (and assigns a new guid for Id), however, it doesn't associate that parent id to the children objects in the `SQL it tries to execute. I came up with a quick and dirty hack, which I list below - But I was wondering, is there something I'm missing here?</p>
  
  <pre><code>IList&lt;BackerEntry&gt; backersTemp = new List&lt;BackerEntry&gt;();
  foreach (BackerEntry backerEntry in jsonBackerEntity.BackerEntries)
  {
    backersTemp.Add(backerEntry);
  }
  
  jsonBackerEntity.BackerEntries.Clear();
  
  foreach (BackerEntry backerEntry in backersTemp)
  {
    jsonBackerEntity.AddChild(backerEntry);
  }
  </code></pre>
  
  <p>Doing it that way is the only way I can seem to get NHibernate to see that these children really belong to this parent. Inside my AddChild method looks like this: </p>
  
  <pre><code>public virtual void AddChild(BackerEntry backerEntry)
  {
    if (backerEntry.Backer != null)
    {
      backerEntry.Backer.BackerEntries.Remove(backerEntry);
    }
    backerEntry.Backer = this;
    this.BackerEntries.Add(backerEntry);
  }
  </code></pre>
  
  <p>EDIT: I think I may have just realized why - Probably because I am not sending back the Parent property of the child in the JSON. I'm not even sure if that'd be possible, due to the circular nature of the two. Child has a parent (who in json has a child who is the original child who has a parent, etc)... Any ideas?</p>