title: fluent nhibernate one to many mapping
body: <p>I am trying to figure out what I thought was just a simple one to many mapping using fluent Nhibernate. I hoping someone can point me to the right directory to achieve this one to many relations
  I have an articles table and a categories table
  Many Articles can only belong to one Category
  Now my Categores table has 4 Categories and Articles has one article associated with cateory1</p>
  
  <p>here is my setup. </p>
  
  <pre><code>using FluentNHibernate.Mapping;
  using System.Collections;
  using System.Collections.Generic;
  
  namespace FluentMapping
  {
      public class Article
      {
  
          public virtual int Id { get; private set; }
          public virtual string Title { get; set; }
          public virtual Category Category{get;set;}
      }
      public class Category
      {
          public virtual int Id { get; private set; }
          public virtual string Description { get; set; }
          public virtual IList&lt;Article&gt; Articles { get; set; }
          public Category()
          {
              Articles=new List&lt;Article&gt;();
          }
  
          public virtual void AddArticle(Article article)
          {
              article.Category = this;
              Articles.Add(article);
          }
          public virtual void RemoveArticle(Article article)
          {
              Articles.Remove(article);
          }
  
      }
      public class ArticleMap:ClassMap&lt;Article&gt;
      {
          public ArticleMap()
          {
              Table("Articles");
              Id(x =&gt; x.Id).GeneratedBy.Identity();
              Map(x =&gt; x.Title);
              References(x =&gt; x.Category).Column("CategoryId").LazyLoad();
  
          }
          public class CategoryMap:ClassMap&lt;Category&gt;
          {
              public CategoryMap()
              {
                  Table("Categories");
                  Id(x =&gt; x.Id).GeneratedBy.Identity();
                  Map(x =&gt; x.Description);
                  HasMany(x =&gt; x.Articles).KeyColumn("CategoryId").Fetch.Join();
              }
          }
      }
  }
  </code></pre>
  
  <p>if I run this test</p>
  
  <pre><code>[Fact]
      public void Can_Get_Categories()
      {
          using (var session = SessionManager.Instance.Current)
          {
              using (var transaction = session.BeginTransaction())
              {
                  var categories = session.CreateCriteria(typeof(Category))
                                //.CreateCriteria("Articles").Add(NHibernate.Criterion.Restrictions.EqProperty("Category", "Id"))                                
                  .AddOrder(Order.Asc("Description"))
                                .List&lt;Category&gt;();
  
              }
          }
      }
  </code></pre>
  
  <p>I am getting 7 Categories due to Left outer join used by Nhibernate
  any idea what I am doing wrong in here?
  Thanks
  [Solution]
  After a couple of hours reading nhibernate docs I here is what I came up with</p>
  
  <pre><code>var criteria = session.CreateCriteria(typeof (Category));
                      criteria.AddOrder(Order.Asc("Description"));
                      criteria.SetResultTransformer(new DistinctRootEntityResultTransformer());
  var cats1 = criteria.List&lt;Category&gt;();
  </code></pre>
  
  <p>Using Nhibernate linq provider </p>
  
  <pre><code> var linq = session.Linq&lt;Category&gt;();
                      linq.QueryOptions.RegisterCustomAction(c =&gt; c.SetResultTransformer(new DistinctRootEntityResultTransformer()));
                      var cats2 = linq.ToList();
  </code></pre>