NHibernate常见问题及解决方法

曾经学过NHibernate的,但是自从工作到现在快一年了却从未用到过,近来要巩固一下却发现忘记了许多,一个“in expected: <end-of-text> (possibly an invalid or unmapped class name was used in the query).”错误查了好半天终于查到了。这篇文章是我转载的NHibernate的常见错误。。。

  • 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".

  • in expected: <end-of-text> (possibly an invalid or unmapped class name was used in the query).

    映射文件没有将属性设为"嵌入的资源"或者调方法时表名大小写问题

  • "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 .
  • IDENTITY_INSERT 为OFF
    在查询分析器中SET IDENTITY_INSERT 表名 ON
  • 在容器中未找到数据项。容器必须实现 IDataItemContainer 或者具有名为 DataItem 的属性。
    <id name="d_name" column="d_name" type="System.String">
          <generator class="assigned"/>
    </id>主键字段必须为当前表存在的字段

    
    

    The element 'class' in namespace 'urn:nhibernate-mapping-2.2' has invalid child element 'property' in namespace 'urn:nhibernate-mapping-2.2'. List of possible elements expected: 'urn:nhibernate-mapping-2.2:meta urn:nhibernate-mapping-2.2:jcs-cache urn:nhibernate-mapping-2.2:cache urn:nhibernate-mapping-2.2:id urn:nhibernate-mapping-2.2:composite-id'.

上一篇:JS的正则表达式及回文


下一篇:jmeter(十三)常见问题及解决方法