title: NHibernate: Invalid Cast (check your mapping for property type mismatches);
body: <p>I have a class ProductCategoryModel:</p>
  
  <pre><code>public class ProductCategoryModel
  {
      public virtual int id { get; set; }
      public virtual string name { get; set; }
      public virtual int parentId { get; set; }
      public virtual Iesi.Collections.Generic.ISet&lt;ProductCategoryModel&gt; subCategory { get; set; }
  }
  </code></pre>
  
  <p>And here is my mapping xml:</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="utf-8" ?&gt;
  &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="Shop.Domain.Model.ProductCategory" assembly="Shop.Domain"&gt;
    &lt;class name="ProductCategoryModel"&gt;
      &lt;id name="id" column="id"&gt;
        &lt;generator class="native" /&gt;
      &lt;/id&gt;
      &lt;property name="name" not-null="true" length="50" type="string" column="name"/&gt;
      &lt;many-to-one name="parentId" not-null="true" class="ProductCategoryModel" column="parentId"/&gt;
      &lt;set name="subCategory" inverse="true"&gt;
        &lt;key column="parentId"/&gt;
        &lt;one-to-many class="ProductCategoryModel"/&gt;
      &lt;/set&gt;
    &lt;/class&gt;
  &lt;/hibernate-mapping&gt;
  </code></pre>
  
  <p>My table on database contains:</p>
  
  <pre><code>id: 2, name: "...", parentId: 0
  id: 3, name: "...", parentId: 2 
  id: 4, name: "...", parentId: 2
  </code></pre>
  
  <p>Now I want to select my category with id 2:</p>
  
  <pre><code>session1.Get&lt;ProductCategoryModel&gt;(id);
  </code></pre>
  
  <p>but Nhibernate throw exception:</p>
  
  <pre><code>        NHibernate.PropertyAccessException was unhandled
    HResult=-2146232832
    Message=Invalid Cast (check your mapping for property type mismatches); setter of Shop.Domain.Model.ProductCategory.ProductCategoryModel
    Source=NHibernate
    StackTrace:
         w NHibernate.Tuple.Entity.PocoEntityTuplizer.SetPropertyValuesWithOptimizer(Object entity, Object[] values)
         w NHibernate.Tuple.Entity.PocoEntityTuplizer.SetPropertyValues(Object entity, Object[] values)
         w NHibernate.Persister.Entity.AbstractEntityPersister.SetPropertyValues(Object obj, Object[] values, EntityMode entityMode)
         w NHibernate.Engine.TwoPhaseLoad.InitializeEntity(Object entity, Boolean readOnly, ISessionImplementor session, PreLoadEvent preLoadEvent, PostLoadEvent postLoadEvent)
         w NHibernate.Loader.Loader.InitializeEntitiesAndCollections(IList hydratedObjects, Object resultSetId, ISessionImplementor session, Boolean readOnly)
         w NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies, IResultTransformer forcedResultTransformer)
         w NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies, IResultTransformer forcedResultTransformer)
         w NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies)
         w NHibernate.Loader.Loader.LoadEntity(ISessionImplementor session, Object id, IType identifierType, Object optionalObject, String optionalEntityName, Object optionalIdentifier, IEntityPersister persister)
         w NHibernate.Loader.Entity.AbstractEntityLoader.Load(ISessionImplementor session, Object id, Object optionalObject, Object optionalId)
         w NHibernate.Loader.Entity.AbstractEntityLoader.Load(Object id, Object optionalObject, ISessionImplementor session)
         w NHibernate.Persister.Entity.AbstractEntityPersister.Load(Object id, Object optionalObject, LockMode lockMode, ISessionImplementor session)
         w NHibernate.Event.Default.DefaultLoadEventListener.LoadFromDatasource(LoadEvent event, IEntityPersister persister, EntityKey keyToLoad, LoadType options)
         w NHibernate.Event.Default.DefaultLoadEventListener.DoLoad(LoadEvent event, IEntityPersister persister, EntityKey keyToLoad, LoadType options)
         w NHibernate.Event.Default.DefaultLoadEventListener.Load(LoadEvent event, IEntityPersister persister, EntityKey keyToLoad, LoadType options)
         w NHibernate.Event.Default.DefaultLoadEventListener.ProxyOrLoad(LoadEvent event, IEntityPersister persister, EntityKey keyToLoad, LoadType options)
         w NHibernate.Event.Default.DefaultLoadEventListener.OnLoad(LoadEvent event, LoadType loadType)
         w NHibernate.Impl.SessionImpl.FireLoad(LoadEvent event, LoadType loadType)
         w NHibernate.Impl.SessionImpl.Get(String entityName, Object id)
         w NHibernate.Impl.SessionImpl.Get(Type entityClass, Object id)
         w NHibernate.Impl.SessionImpl.Get[T](Object id)
         w Shop.Infrastructure.Repositories.ProductCategoryIM.GetCategoryById(Int32 id) w f:\Uczelnia\!Semestr VI\Kurs aplikacji bazodanowych\KubutShop2\KubutShop\Shop.Infrastructure\Repositories\ProductCategoryIM.cs:wiersz 58
         w Shop.Application.Front.FrontService.GetProductCategoryById(Int32 id) w f:\Uczelnia\!Semestr VI\Kurs aplikacji bazodanowych\KubutShop2\KubutShop\Shop.Application\Front\Front.cs:wiersz 139
         w Presentation.ConsoleApp.Program.Main(String[] args) w f:\Uczelnia\!Semestr VI\Kurs aplikacji bazodanowych\KubutShop2\KubutShop\Presentation.ConsoleApp\Program.cs:wiersz 40
         w System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
         w System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
         w Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
         w System.Threading.ThreadHelper.ThreadStart_Context(Object state)
         w System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
         w System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
         w System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
         w System.Threading.ThreadHelper.ThreadStart()
    InnerException: System.InvalidCastException
         HResult=-2147467262
         Message=Nie można rzutować obiektu typu 'NHibernate.Collection.Generic.PersistentGenericSet`1[Shop.Domain.Model.ProductCategory.ProductCategoryModel]' na typ 'Iesi.Collections.Generic.ISet`1[Shop.Domain.Model.ProductCategory.ProductCategoryModel]'.
         Source=Shop.Domain
         StackTrace:
              w (Object , Object[] , SetterCallback )
              w NHibernate.Bytecode.Lightweight.AccessOptimizer.SetPropertyValues(Object target, Object[] values)
              w NHibernate.Tuple.Entity.PocoEntityTuplizer.SetPropertyValuesWithOptimizer(Object entity, Object[] values)
         InnerException: 
  </code></pre>
  
  <blockquote>
    <p>Message=Nie można rzutować obiektu typu 'NHibernate.Collection.Generic.PersistentGenericSet<code>1[Shop.Domain.Model.ProductCategory.ProductCategoryModel]' na typ Iesi.Collections.Generic.ISet</code>1[Shop.Domain.Model.ProductCategory.ProductCategoryModel]'.</p>
  </blockquote>
  
  <p>means something like this:</p>
  
  <blockquote>
    <p>Message=Cannot cast 'NHibernate.Collection.Generic.PersistentGenericSet<code>1[Shop.Domain.Model.ProductCategory.ProductCategoryModel]' to type Iesi.Collections.Generic.ISet</code>1[Shop.Domain.Model.ProductCategory.ProductCategoryModel]'</p>
  </blockquote>
  
  <p>What I'm doing wrong? I read a lot of similar topics, but I still have no idea where is a mistake. 
  P.S.
  Sorry for my terrible English ;)</p>