曾经学过NHibernate的,但是自从工作到现在快一年了却从未用到过,近来要巩固一下却发现忘记了许多,一个“in expected: <end-of-text> (possibly an invalid or unmapped class name was used in the query).”错误查了好半天终于查到了。这篇文章是我转载的NHibernate的常见错误。。。
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".
映射文件没有将属性设为"嵌入的资源"或者调方法时表名大小写问题
"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>
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()
解决方法:
该异常的方法比较简单,在程序集中添加引用就可以了.
感觉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" />
引起这个错误的一个原因是数据库锁定,NHibernate默认采用的是乐观锁定.关于NHibernate中乐观锁定以及如何解决错误的信息可以看there .
在查询分析器中SET IDENTITY_INSERT 表名 ON
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‘.
原文:http://www.cnblogs.com/xdot/p/5968003.html