title: NHibernate one-to-one: null id generated for AccountDetail
body: <p>I got an exception "<strong>null id generated for AccountDetail</strong>" when mapping one-to-one relationship by using many-to-one with unique constraint.</p>
  
  <p><strong>Here's my SQL tables:</strong></p>
  
  <pre><code>Account(Id, Name)
  AccountDetail(AccountId, Remark)
  </code></pre>
  
  <p>AccountId is both primary and foreign key.</p>
  
  <p><strong>Here's my Domain Model (Account and AccountDetail):</strong></p>
  
  <pre><code>public class Account
  {
      public virtual int Id { get; set; }
  
      public virtual string Name { get; set; }
  
      public virtual AccountDetail Detail { get; set; }
  
      public Account()
      {
          Detail = new AccountDetail
          {
              Account = this
          };
      }
  }
  
  public class AccountDetail
  {
      public virtual int AccountId { get; set; }
  
      public virtual Account Account { get; set; }
  
      public virtual string Remark { get; set; }
  }
  </code></pre>
  
  <p><strong>Mapping (NHibenrate 3.3 mapping by code):</strong></p>
  
  <pre><code>class AccountMap : ClassMapping&lt;Account&gt;
  {
      public AccountMap()
      {
          Table(typeof(Account).Name);
  
          Id(c =&gt; c.Id, m =&gt; m.Generator(Generators.Native));
  
          Property(c =&gt; c.Name);
  
          OneToOne(c =&gt; c.Detail, m =&gt;
          {
              m.Constrained(true);
              m.Cascade(Cascade.All);
            m.PropertyReference(typeof(AccountDetail).GetPropertyOrFieldMatchingName("Account"));
          });
      }
  }
  
  class AccountDetailMap : ClassMapping&lt;AccountDetail&gt;
  {
      public AccountDetailMap()
      {
          Table(typeof(AccountDetail).Name);
  
          Id(c =&gt; c.AccountId, m =&gt;
          {
              m.Column("AccountId");
              m.Generator(Generators.Foreign&lt;AccountDetail&gt;(x =&gt; x.Account));
          });
  
          Property(c =&gt; c.Remark);
  
          ManyToOne(c =&gt; c.Account, m =&gt;
          {
              m.Column("AccountId");
              m.Unique(true);
          });
      }
  }
  </code></pre>
  
  <p><strong>BTW:</strong> Can I remove the AccountId property in AccountDetail? That is, only use the Account property. Using both AccountId and Account properties in AccountDetail class looks not so object-oriented.</p>
  
  <p>Thanks!</p>