运行程序和遇到的一些异常

一、 常见异常与排查原因

  1. TypeInitializationException (mscorlib.dll)
    • 原因:配置文件未正确配置或被程序读取到(如文件名、路径、生成属性错误)。需确保文件名为 hibernate.cfg.xml 且属性设置为“始终复制”。
  2. Could not create the driver from NHibernate.Driver.SqlServerCeDriver
    • 原因:曾误用 SQL Server Compact (CE) 驱动或旧版写法。常规 SQL Server 应改为 NHibernate.Driver.SqlClientDriver
  3. 方言 (Dialect) 匹配
    • 出现了 MsSqlCeDialect,实际常规 SQL Server 应使用如 NHibernate.Dialect.MsSql2012Dialect(或对应版本),避免数据库语法兼容问题。
  4. 映射程序集缺失
    • 原因:未配置 <mapping assembly="NHibernateDemo"/>,导致 NHibernate 无法找到实体与表的 .hbm.xml 映射文件。必须指定当前项目程序集名。
  5. 映射文件版本号
    • 映射文件 (News.hbm.xml) 中的命名空间需匹配:xmlns="urn:nhibernate-mapping-2.2",教程强调改为 2.2 以适配。

二、 最终正确的配置参考

1. hibernate.cfg.xml(根目录,始终复制)

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <!-- 常规 SQL Server 方言(按实际版本调整,如 MsSql2012Dialect) -->
    <property name="dialect">NHibernate.Dialect.MsSql2012Dialect</property>
    <!-- 正确的 SQL Client 驱动 -->
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <!-- 连接字符串(按本地实际修改) -->
    <property name="connection.connection_string">Data Source=.;Initial Catalog=NHibernateDemo;Integrated Security=True;Pooling=False</property>
    <property name="show_sql">true</property>
    <!-- 关键:指定映射文件所在程序集 -->
    <mapping assembly="NHibernateDemo"/>
  </session-factory>
</hibernate-configuration>Code language: HTML, XML (xml)

2. 映射文件 (News.hbm.xml) 头部确认

位于 Mappings 文件夹,属性设为“嵌入的资源”:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernateDemo" namespace="NHibernateDemo.Models">
  <class name="NHibernateDemo.Models.News, NHibernateDemo" table="News">
    <!-- ...字段映射... -->
  </class>
</hibernate-mapping>Code language: HTML, XML (xml)

三、 运行成功与数据读取

排除上述配置问题后,在 Main 方法中调用:

session = sessionManager.GetSession(); // 或 OpenSession()
newsDal = new NewsDAL(session);
News newsInfo = newsDal.GetNewsByID(1);
Console.WriteLine(newsInfo.Title); // 输出:"Foxdevelop欢迎"
Console.ReadKey();Code language: JavaScript (javascript)

Unable to locate persister for the entity named ‘NHibernateDemo.Models.News’.\r\nThe persister define the persistence strategy for an entity.\r\nPossible causes:\r\n – The mapping for ‘NHibernateDemo.Models.News’ was not added to the NHibernate configuration.

Unable to locate persister for the entity named ‘NHibernateDemo.Models.News’.

The persister define the persistence strategy for an entity.

Possible causes:

 – The mapping for ‘NHibernateDemo.Models.News’ was not added to the NHibernate configuration.

上面错误找到原因了

<mapping assembly=“DomainModel”/>
就是需要再hibernate.cfg.xml里面增加

<mapping assembly=”NHibernateDemo”/>

只有这样 nhibernate才能去找到实体和表的映射文件

配置后现在又报这个错误

{“Could not compile the mapping document: NHibernateDemo.Mappings.News.hbm.xml”}

观察异常信息后 发现这些

{“不应有 <hibernate-mapping xmlns=’urn:nhibernate-mapping-2.1′>。”}

“XML 文档(1, 2)中有错误。”

这里改为2.2

这样之后 大功告成

我们可以查找刚才的那条数据

下面把这几个配置文件贴一下,首先是hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="dialect">NHibernate.Dialect.MsSqlCeDialect</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="connection.connection_string">Data Source=.;Initial Catalog=NHibernateDemo;Integrated Security=True;Pooling=False</property>
    <property name="show_sql">true</property>
    <mapping assembly="NHibernateDemo"/>
  </session-factory>
</hibernate-configuration>
Code language: HTML, XML (xml)
然后是News.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping
  xmlns="urn:nhibernate-mapping-2.2"
  assembly="NHibernateDemo"
  namespace="NHibernateDemo.Models">
  <class name="NHibernateDemo.Models.News,NHibernateDemo" table="News">
    <id name="ID" column="ID" type="Int32" unsaved-value="0">
      <generator class="native"></generator>
    </id>
    <property name="Title" column="Title" type="string" length="150" not-null="true"/>
    <property name="Content" column="Content" type="string" not-null="true"/>
    <property name="AddTime" column="AddTime" type="DateTime" not-null="true"/>
    <property name="Status" column="Status" type="bool" not-null="true"/>
  </class>
</hibernate-mapping>
Code language: HTML, XML (xml)

An exception occurred during configuration of persistence layer.“

把hibernate.cfg.xml的属性改为始终复制 内容

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注