NHibernate常见问题及解决方法

  1. 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的类型初始值设定项引发异常".
  2. 在对照类中如果属性没有加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".

  3. 映射文件没有将属性设为"嵌入的资源"引起的错误.

    这个是我最常忽略的,如果忽略了它,可能会报比较多的错误,其中一个就是如下形式:

    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]

  4. "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>
  5. "未能未能加载文件或程序集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()

    解决方法:

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

  6. 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" />
  7. Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect)
    引起这个错误的一个原因是数据库锁定,NHibernate默认采用的是乐观锁定.关于NHibernate中乐观锁定以及如何解决错误的信息可以看there .

你可能感兴趣的:(Hibernate)