NH的学习 之一 xml文件的属性之嵌入性资源的错误

hbm.xmlnhibernate文件中版本号可能引起的问题.
< hibernate-mapping xmlns=" urn:nhibernate-mapping-2.2" >          
    此处的2.2代表了nhibernate的版本号 必须与你安装的nhibernate的产品版本号相符.否则的话 举个例子 若为urn:nhibernate-mapping-2.0 则会出现如下错误: could not find schema information for the element ' urn:nhibernate-mapping-2.0:hibernate-mapping' . 或是:" nhibernate.cfg.environment的类型初始值设定项引发异常" .
在对照类中如果属性没有加virtual关键字 可能报
nhibernate.invalidproxytypeexception: the following types may not be used as proxies:
model.friendlink: method set_description should be virtual
model.type: method get_typename should be virtual.....
这是一种解决方案是给属性加上virtual关键字 另一种解决方法是在映射文件中加入default-lazy=" false" .

映射文件没有将属性设为"嵌入的资源"引起的错误.
这个是我最常忽略的,如果忽略了它,可能会报比较多的错误,其中一个就是如下形式:

nhibernate.queryexception: in expected: < end-of-text>   (possibly an invalid or unmapped class name was used in the query) [from post order by issetup desc   datetime desc   hot desc]

" could not find the dialect in the configuration" 异常
异常描述:
nhibernate.mappingexception: could not compile the mapping document: model.friendlink.hbm.xml --->   system.invalidoperationexception: could not find the dialect in the configuration
    在 nhibernate.dialect.dialect.getdialect(idictionary`2 props)
    在 nhibernate.cfg.configuration.addvalidateddocument(namedxmldocument doc)
解决方法:
配置文件中xmlns=" urn:nhibernate-configuration-2.2" 千万不能忘记 确保没有忘掉xmlns=" urn:nhibernate-configuration-2.2" 就可以解决这个bug.

< hibernate-configuration xmlns=" urn:nhibernate-configuration-2.2" >    < session-factory>      < property name=" connection.provider" > nhibernate.connection.driverconnectionprovider< /property>      < property name=" use_outer_join" > true< /property>      < property name=" connection.driver_class" > nhibernate.driver.sqlclientdriver< /property>      < property name=" show_sql" > true< /property>      < property name=" dialect" > nhibernate.dialect.mssql2005dialect< /property>      < property name=" connection.connection_string" > server=.\sqlexpress initial catalog=sblog user id=sa password=1 < /property>      < mapping assembly=" xmgl.model" />    < /session-factory> < /hibernate-configuration>
" 未能未能加载文件或程序集castle.dynamicproxy2" 的异常
异常描述:
system.typeinitializationexception: “nhibernate.proxy.poco.castle.castleproxyfactory”的类型初始值设定项引发异常。 --->   system.io.filenotfoundexception: 未能加载文件或程序集“castle.dynamicproxy2   version=2.0.3.0   culture=neutral   publickeytoken=407dd0808d44fbdc”或它的某一个依赖项。系统找不到指定的文件。
文件名:“castle.dynamicproxy2   version=2.0.3.0   culture=neutral   publickeytoken=407dd0808d44fbdc”
     在 nhibernate.proxy.poco.castle.castleproxyfactory..cctor()

解决方法:

该异常的方法比较简单 在程序集中添加引用就可以了.

timestamp的使用.
感觉nhibernate对timestamp支持不好 我在sql server 2005定义了一个timestamp类型的列 在映射文件里映射为datetime类型 然后就报:
     nhibernate.adoexception: could not cast the value in field upsize2_0_ of type byte[] to the type timestamptype.   please check to make sure that the mapping is correct and that your dataprovider supports this data type. --->   system.invalidcastexception: 无法将类型为“system.byte[]”的对象强制转换为类型“system.iconvertible”。
     在 system.convert.todatetime(object value)
     在 nhibernate.type.timestamptype.get(idatareader rs   int32 index)
     在 nhibernate.type.nullabletype.nullsafeget(idatareader rs   string name)
搞了半天也没有配好 只能将数据库列改为datetime类型 然后以下面的格式配置映射文件:

< id name=" contentid"   type=" string"   unsaved-value=" null" >
              < column name=" contentid"   length=" 36"   sql-type=" nvarchar"   not-null=" true"   unique=" true"   index=" aaaaacontent_pk" />
        < generator class=" assigned" />
          < /id>
      < timestamp name=" upsizets"   column=" upsize_ts"   />
row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)
引起这个错误的一个原因是数据库锁定 nhibernate默认采用的是乐观锁定.关于nhibernate中乐观锁定以及如何解决错误的信息可以看there .

posted @ 2009-12-17 11:18  妖*小夜  阅读(326)  评论(0编辑  收藏  举报