c# - NHibernate: Invalid Cast (check your mapping for property type mismatches); -
i have class productcategorymodel:
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<productcategorymodel> subcategory { get; set; } }
and here mapping xml:
<?xml version="1.0" encoding="utf-8" ?> <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="shop.domain.model.productcategory" assembly="shop.domain"> <class name="productcategorymodel"> <id name="id" column="id"> <generator class="native" /> </id> <property name="name" not-null="true" length="50" type="string" column="name"/> <many-to-one name="parentid" not-null="true" class="productcategorymodel" column="parentid"/> <set name="subcategory" inverse="true"> <key column="parentid"/> <one-to-many class="productcategorymodel"/> </set> </class> </hibernate-mapping>
my table on database contains:
id: 2, name: "...", parentid: 0 id: 3, name: "...", parentid: 2 id: 4, name: "...", parentid: 2
now want select category id 2:
session1.get<productcategorymodel>(id);
but nhibernate throw exception:
nhibernate.propertyaccessexception unhandled hresult=-2146232832 message=invalid cast (check mapping 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:
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]'.
means this:
message=cannot cast 'nhibernate.collection.generic.persistentgenericset
1[shop.domain.model.productcategory.productcategorymodel]' type iesi.collections.generic.iset
1[shop.domain.model.productcategory.productcategorymodel]'
what i'm doing wrong? read lot of similar topics, still have no idea mistake. p.s. sorry terrible english ;)
with latest 4.0+ nhibernate version should keep in mind crucial change, described here, in q & a
let me cite bit (from release notes):
** known breaking changes nh3.3.3.ga 4.0.0.ga
nhibernate targets .net 4.0. many uses of set types iesi.collections have been changed use corresponding types bcl. api these types different.
so - use system
not iesi
stuff, e.g.:
system.collections.generic.iset<t>
the second thing, important, need create bi-directional mapping, one-to-many
, many-to-one
. means cannot
// not iesi // need system using system.collections.generic; using system.collections.objectmodel; ... public class productcategorymodel { ... // used navigation property public virtual int parentid { get; set; } // must inverse="true" public virtual productcategorymodel parent { get; set; } // system.collections.generic public virtual iset<productcategorymodel> subcategories { get; set; } ...
and mapping:
... // readonly <property name="parentid" not-null="true" column="parentid" type="string" insert="false" update="false" /> // inverse end <many-to-one name="parent" not-null="true" class="productcategorymodel" column="parentid"/> <set name="subcategories" inverse="true" batch-size="25"> <key column="parentid"/> <one-to-many class="productcategorymodel"/> </set> ...
as can see, can have mapping both many-to-one
(reference) , property
(valuetype or string)
. 1 of them must readonly (insert="false" update="false")
note: not forget, because use inverse, have set both sides of relation in c#...
Comments
Post a Comment