title: NHibernate mapping by code ManyToOne with CompositeIdentity
body: <p>I am trying to convert my FluentNHibernate mappings to NHibernate Mapping By-code using NHibernate 3.3.3. The goal is to upgrade to NHibernate 3.3.3 and to cut down on the number of assemblies being distributed. I am having some trouble with translating FluentNHibernate's References mapping to a Many-To-One mapping.</p>
  
  <p>Many of my entities have descriptions that need translations. For this I use a Texts table that contains these texts in all available languages. I use the text ID to reference the Texts table and then in the Data Access Object I filter the required language. This works create using NHibernate 3.1 and FluentNHibernate, with NHibernate 3.3.3 and mapping by-code however I just a MappingException saying : property mapping has wrong number of columns: Category.Description type: Text.</p>
  
  <p>Where is my new mapping wrong? Or is this type of mapping not possible in NHibernate 3.3.3.</p>
  
  <p>This is the Texts table (SQL-server 2008).</p>
  
  <pre class="lang-sql prettyprint-override"><code>CREATE TABLE Texts (
      ID         int           NOT NULL,
      languageID nvarchar(10)  NOT NULL,
      Singular   nvarchar(max) NOT NULL,
      Plural     nvarchar(max) NULL,
  CONSTRAINT PK_Texts PRIMARY KEY CLUSTERED (ID ASC, languageID ASC)
  WITH (PAD_INDEX              = OFF,
        STATISTICS_NORECOMPUTE = OFF,
        IGNORE_DUP_KEY         = OFF,
        ALLOW_ROW_LOCKS        = ON,
        ALLOW_PAGE_LOCKS       = ON) ON [PRIMARY]) ON [PRIMARY]
  </code></pre>
  
  <p>The Text class:</p>
  
  <pre class="lang-csharp prettyprint-override"><code>public class Text
  {
      public Text(int id, string language, string singular, string plural)
      {
          this.ID = new TextCompositeID(id, language);
          this.Singular = singular;
          this.Plural = plural;
      }
      public TextCompositeID ID { get; private set; }
      public string Plural { get; private set; }
      public string Singular { get; set; }
      public override bool Equals(object obj)
      {
          var text = (Text)obj;
          if (text == null)
          {
              return false;
          }
          return this.ID.Equals(text.ID);
      }
      public override int GetHashCode()
      {
          return this.ID.GetHashCode();
      }
  }
  </code></pre>
  
  <p>As an example here is the Category class:</p>
  
  <pre class="lang-csharp prettyprint-override"><code>public class Category
  {
      public int ID { get; set; }
      public Text Description { get; set; }
  }
  </code></pre>
  
  <p>The FluentNHibernate xml mapping for the Category class looks like this:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;class xmlns="urn:nhibernate-mapping-2.2"
         mutable="true"
         name="Category"
         lazy="false"
         table="Category"
         where="IsObsolete=0"&gt;
      &lt;id name="ID" type="System.Int32"&gt;
          &lt;column name="ID" not-null="true" /&gt;
          &lt;generator class="native" /&gt;
      &lt;/id&gt;
      &lt;many-to-one cascade="none"
                   class="Text"
                   name="Description"&gt;
          &lt;column name="TextID"
                  not-null="true"
                  unique="false" /&gt;
      &lt;/many-to-one&gt;
  &lt;/class&gt;
  </code></pre>
  
  <p>Which was generated from this:</p>
  
  <pre class="lang-csharp prettyprint-override"><code>public class CategoryMap : ClassMap&lt;Category&gt;
  {
      public CategoryMap()
      {
          this.Table("Category");
          Not.LazyLoad();
          this.Where("IsObsolete=0");
          Id(x =&gt; x.ID)
              .Column("ID")
              .GeneratedBy.Native()
              .Not.Nullable();
          References(x =&gt; x.Description)
              .Column("DescriptionID")
              .Cascade.None()
              .Not.Unique()
              .Not.Nullable();
      }
  }
  </code></pre>
  
  <p>This is the NHibernate ClassMapping I created:</p>
  
  <pre class="lang-csharo prettyprint-override"><code>public CategoryMap()
  {
      this.Lazy(false);
      this.Mutable(true);
      this.Table("Category");
      this.Where("IsObsolete=0");
      this.Id(
          x =&gt; x.ID,
          map =&gt;
          {
              map.Column("ID");
              map.Generator(Generators.Native);
          });
      this.ManyToOne(
          x =&gt; x.Description,
          map =&gt;
          {
              map.Cascade(Cascade.None);
              map.Class(typeof(Text));
              map.Column("TextID");
              map.Fetch(FetchKind.Join);
              map.Lazy(LazyRelation.NoLazy);
              map.ForeignKey("none");
          });
  }
  </code></pre>
  
  <p>From this I get this xml mapping:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;class name="Category"
         lazy="false"
         table="Category"
         where="IsObsolete=0"&gt;
      &lt;id name="ID"
          column="ID"
          type="Int32"&gt;
          &lt;generator class="native" /&gt;
      &lt;/id&gt;
      &lt;many-to-one name="Description"
                   class="Text"
                   column="TextID"
                   fetch="join"
                   foreign-key="none"
                   lazy="false" /&gt;
  &lt;/class&gt;
  </code></pre>