曾經學過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'.