title: Invalid Cast (check your mapping for property type mismatches)
body: <p><strong>Scenario</strong><br/>
  I  have a category class in DB:</p>
  
  <pre><code>CREATE TABLE [dbo].[Category](
      [pk_cat_id] [int] NOT NULL,
      [name] [varchar](50) NOT NULL,
      [parent_cat_id] [int] NULL
   CONSTRAINT [PK_Category] PRIMARY KEY NONCLUSTERED 
  (
      [pk_cat_id] ASC
  ))
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/ZVfeN.png" alt="enter image description here"></p>
  
  <p>Category class has association to itself. It is a recursive, bidirectional association(many-to-one and one-to-many).
  Both refer to the same foreign key column: parent_cat_id.<br/>
  A category can have one parent at-most and no or more child categories.<br/></p>
  
  <p>This is Category.hbm.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;hibernate-mapping xmlns ="urn:nhibernate-mapping-2.2"
                     assembly ="NHibernateIntro.Core"
                     namespace ="NHibernateIntro.Core.Domain"&gt;
  
    &lt;class name="Category" table="Category"&gt;
  
      &lt;id name="CategoryId" column="pk_cat_id"&gt;
        &lt;generator class="hilo"/&gt;
      &lt;/id&gt;
  
      &lt;property name="Name" column="name" type="string" length="50" not-null="true" /&gt;
  
      &lt;many-to-one name="ParentCategory" class="Category" column="parent_cat_id" /&gt;
  
      &lt;bag name="childCategories" cascade="all-delete-orphan" inverse="true"&gt;
        &lt;key column="parent_cat_id"/&gt;
        &lt;one-to-many class="Category"/&gt;      
      &lt;/bag&gt;
  
    &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>This is Category.cs:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using Iesi.Collections.Generic;
  
  namespace NHibernateIntro.Core.Domain
  {
      public class Category
      {
          private Category parent_category;
          private ISet&lt;Category&gt; child_Categories = new HashedSet&lt;Category&gt;();
  
          public virtual int CategoryId { get; set; }
          public virtual string Name { get; set; }
  
          public Category() { }
  
          public Category( string cat_name )
          {
              Name = cat_name;
          }
  
          public virtual Category ParentCategory 
          {
              get
              {
                  if (parent_category == null)
                      parent_category = new Category();
  
                  return parent_category;
              }
              set{ parent_category = value; }
          }
  
          public virtual ISet&lt;Category&gt; childCategories
          {
              get { return child_Categories; }
              set { child_Categories = value; }
          } 
      }
  }
  </code></pre>
  
  <p>This is the Main method:</p>
  
  <pre><code>public static void Run(ISessionFactory factory)
  {
      int computerId = 1;
  
      using (ISession session = factory.OpenSession())
      using (session.BeginTransaction())
      {
          Category computer = session.Get&lt;Category&gt;(computerId); // **This line causes   Error(stated below)**
  // Please see 'CONFUSING' tag below.
              Category laptops = new Category("Laptops");
              computer.childCategories.Add(laptops);
              laptops.ParentCategory = computer;
              session.Save(laptops);
              session.Transaction.Commit();
          }
      }
  </code></pre>
  
  <p><strong>CONFUSING</strong> : When i debugged the code it stuck here at this line: "set{ parent_category = value; }". I am confused because I am assigning to Cateory then why setter of parentCategory is being called here?</p>
  
  <p><strong>Error:</strong>
  Invalid Cast (check your mapping for property type mismatches);
   setter of NHibernateIntro.Core.Domain.Category</p>
  
  <p><em>Inner Error:</em> Unable to cast object of type'NHibernate.Collection.Generic.PersistentGenericBag<code>1[NHibernateIntro.Core.Domain.Category]'
  to type 'Iesi.Collections.Generic.ISet</code>1[NHibernateIntro.Core.Domain.Category]'.</p>
  
  <p>kINDLY hELP!!</p>