-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: I have an " InvalidCastException " problem
PostPosted: Mon Feb 22, 2010 3:39 am 
Newbie

Joined: Mon Feb 22, 2010 3:22 am
Posts: 2
the stack:

Code:
[InvalidCastException: 无法将类型为([color=#FF0000]Can not cast [/color])“NHibernate.Collection.Generic.PersistentGenericSet`1[qianhu.dadi.model.Catalog]”的对象强制转换为类型([color=#FF0000]strongly into [/color])“System.Collections.Generic.IList`1[qianhu.dadi.model.Catalog]”。]
   (Object , Object[] , SetterCallback ) +210
   NHibernate.Bytecode.Lightweight.AccessOptimizer.SetPropertyValues(Object target, Object[] values) +42
   NHibernate.Tuple.Entity.PocoEntityTuplizer.SetPropertyValuesWithOptimizer(Object entity, Object[] values) +66

[PropertyAccessException: Invalid Cast (check your mapping for property type mismatches); setter of qianhu.dadi.model.Catalog]
   NHibernate.Tuple.Entity.PocoEntityTuplizer.SetPropertyValuesWithOptimizer(Object entity, Object[] values) +157
   NHibernate.Tuple.Entity.PocoEntityTuplizer.SetPropertyValues(Object entity, Object[] values) +82
   NHibernate.Persister.Entity.AbstractEntityPersister.SetPropertyValues(Object obj, Object[] values, EntityMode entityMode) +47
   NHibernate.Engine.TwoPhaseLoad.InitializeEntity(Object entity, Boolean readOnly, ISessionImplementor session, PreLoadEvent preLoadEvent, PostLoadEvent postLoadEvent) +1012
   NHibernate.Loader.Loader.InitializeEntitiesAndCollections(IList hydratedObjects, Object resultSetId, ISessionImplementor session, Boolean readOnly) +390
   NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) +958
   NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) +94
   NHibernate.Loader.Loader.DoList(ISessionImplementor session, QueryParameters queryParameters) +167
   NHibernate.Loader.Loader.ListIgnoreQueryCache(ISessionImplementor session, QueryParameters queryParameters) +38
   NHibernate.Loader.Loader.List(ISessionImplementor session, QueryParameters queryParameters, ISet`1 querySpaces, IType[] resultTypes) +130
   NHibernate.Loader.Criteria.CriteriaLoader.List(ISessionImplementor session) +58
   NHibernate.Impl.SessionImpl.List(CriteriaImpl criteria, IList results) +541
   NHibernate.Impl.CriteriaImpl.List(IList results) +62
   NHibernate.Impl.CriteriaImpl.List() +87
   qianhu.dadi.dal.CatalogDal.checkExistsByName(String name) in G:\website\qianhu\dadi\dadiLib\dal\CatalogDal.cs:65
   qianhu.dadi.service.CatalogService.checkExistsByName(String name) in G:\website\qianhu\dadi\dadiLib\service\CatalogService.cs:34
   forum_addCatalog1.Button1_Click(Object sender, EventArgs e) in g:\website\qianhu\dadi\dadiSite\forum\addCatalog1.aspx.cs:28
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +96
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +116
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +31
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +32
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +72
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3824


My Class:

Code:
using System;
using System.Collections.Generic;
using System.Text;

namespace qianhu.dadi.model
{
    public class Catalog
    {
        /// <summary>
        /// 编号
        /// </summary>
        private int cid;

        public virtual int Cid
        {
            get { return cid; }
            set { cid = value; }
        }
        /// <summary>
        /// 分类名
        /// </summary>
        private String name;

        public virtual String Name
        {
            get { return name; }
            set { name = value; }
        }
        /// <summary>
        /// 父分类
        /// </summary>
        private Catalog parent;

        public virtual Catalog Parent
        {
            get { return parent; }
            set { parent = value; }
        }

        /// <summary>
        /// 子分类
        /// </summary>
        private IList<Catalog> children;

        public virtual IList<Catalog> Children
        {
            get { return children; }
            set { children = value; }
        }



        /// <summary>
        /// 分类
        /// </summary>
        public Catalog()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }


        private IList<Message> messages;

        public virtual IList<Message> Messages
        {
            get { return messages; }
            set { messages = value; }
        }


    }
}



My mapping file:
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
    namespace="qianhu.dadi.model" assembly="qianhu.dadi">

  <class name="Catalog" table="dadi_Catalog">
    <id name="Cid">
      <column name="Cid" not-null="true"/>
      <generator class="identity" />
    </id>

    <property name="Name" type="String"/>

    <many-to-one name="Parent" class="Catalog" column="Parent_ID" lazy="false" not-null="false" />

    <set name="Children" inverse="true" lazy="false">
      <key column="Parent_ID"/>
      <one-to-many class="Catalog"/>
    </set>

    <set name="Messages" inverse="true" lazy="false">
      <key column="Cid"/>
      <one-to-many class="Message"/>
    </set>


  </class>

</hibernate-mapping>


My Code:

Code:
        public bool checkExistsByName(string name)
        {
            ISession session = NHibernateHelper.GetCurrentSession();
            IList<Catalog> catalogs = session.CreateCriteria<Catalog>().Add(Restrictions.Like("Name", name)).List<Catalog>();
            NHibernateHelper.CloseSession();
            if (catalogs.Count > 0)
            {
                return true;
            }
            else {
                return false;
            }
           
        }


I hava tried a lot of places,hereis the last hope. :(


Top
 Profile  
 
 Post subject: Re: I have an " InvalidCastException " problem
PostPosted: Tue Feb 23, 2010 3:49 pm 
Beginner
Beginner

Joined: Tue Aug 25, 2009 11:42 am
Posts: 49
Your mapping says its a SET and u are casting it to a LIST!
Change the mapping:
<list name="Children" inverse="true" lazy="false">
<key column="Parent_ID"/>
<list-index column="LIST_INDEX"/>
<one-to-many class="Catalog"/>
</list>
Or if the order does not matter:
<bag name="Children" inverse="true" lazy="false">
<key column="Parent_ID"/>
<one-to-many class="Catalog"/>
</bag>


Top
 Profile  
 
 Post subject: Re: I have an " InvalidCastException " problem
PostPosted: Tue Feb 23, 2010 3:54 pm 
Beginner
Beginner

Joined: Tue Aug 25, 2009 11:42 am
Posts: 49
see http://stackoverflow.com/questions/671575/iset-vs-ilist too


Top
 Profile  
 
 Post subject: Re: I have an " InvalidCastException " problem
PostPosted: Wed Feb 24, 2010 4:15 am 
Newbie

Joined: Mon Feb 22, 2010 3:22 am
Posts: 2
oh my god. I am not family with List Bag Set etc. But that's the key! Thank you very much with tears on my face.


Top
 Profile  
 
 Post subject: Re: I have an " InvalidCastException " problem
PostPosted: Thu Feb 25, 2010 4:03 am 
Beginner
Beginner

Joined: Sat Jan 26, 2008 6:33 am
Posts: 24
An alternate way to solve this without changing the mapping would be:
Code:
private ICollection<Catalog> children;

Just in case you dont really need the IList functionality.
The online documentation is a bit brief in this section.

Greetings!
Zorgoban


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.