首先我对那些,自己认为被我前天的博文 
接下来先检查Student.hbm.xml中的

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Model.Entities.Student,Model" table="Student" lazy="true">
    <id name="StudentId" column="StudentId" type="int">
      <generator class="native" />
    </id>
    <property type="string" length="40" name="StudentName" column="[StudentName]" />
    <property type="string" length="20" name="StudentPassword" column="[StudentPassword]" />
    <property type="string" length="40" name="EmailAddress" column="[EmailAddress]" />
    <property type="DateTime" name="DateCreated" column="[DateCreated]" />
  </class>
</hibernate-mapping>

 

 

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="Model.Entities.Student,Model" table="Student" lazy="true">



这里要注意是2.2,不是2.0。
其次是在下面的Model.Entities.Student,Model这里前者是实体,后者是程序集。这个当然只有初学者会有点疑问。

接下来是增加应用程序配置文件信息,首先是在App.config里面加上

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <!--<add name="Connection String" connectionString="Data Source=YeanJay;User Id=YeanJay;Password=YeanJay;Persist Security Info=true" providerName="System.Data.OracleClient"/>-->
    <add name="Connection String" connectionString="Data Source=YeanJay;Database=NHibernate;uid=YeanJay;pwd=YeanJay" providerName="System.Data.SqlClient"/>

  </connectionStrings>
</configuration>

 

这里只是增加了数据库链接字符串,注意:这里的Connection String在NHibernate的配置文件中要用到,所以要保持一致。这里如果要将数据库从MSSql迁移到Oracle,那么我们只要将上面的注释去掉,然后将下面这句MSSQL的注释掉就可以了。
然后来看NHibernate的两个配置文件:NHibernateMSSQL.config和NHibernateOracle.config
>NHibernateMSSQL.config

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" >
  <session-factory name="northwind">
    <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="connection.connection_string_name">Connection String</property>
    <property name="connection.isolation">ReadCommitted</property>
    <!-- Mapping Files -->
    <mapping assembly="Model"/>
  </session-factory>
</hibernate-configuration>

 


>NHibernateOracle.config

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration  xmlns="urn:nhibernate-configuration-2.2" >
  <session-factory name="northwind">
    <property name="dialect">NHibernate.Dialect.Oracle10Dialect</property>
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="connection.driver_class">NHibernate.Driver.OracleClientDriver</property>
    <property name="connection.connection_string_name">Connection String</property>
    <property name="connection.isolation">ReadCommitted</property>

    <!-- Mapping Files -->
    <mapping assembly="Model"/>
  </session-factory>
</hibernate-configuration>

 

 

这里有一个<mapping assembly="Model">就是之前提到的那个映射实体类的那个程序集。
在开始编写代码之前,我们需要将一些需要的引用添加到每个工程里面:
DAL:引用Model项目和NHibernate.dll
Model:引用Iesi.Collections.dll和NHibernate.dll
在WinForm项目中:根据YJingLee的例子是引用DAL,Model这两个项目,和NHibernate.dll,暂时我们也这么引用。
现在开始编写代码:
SessionManager.cs类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NHibernate;
using NHibernate.Cfg;
using Model;

namespace DAL
{
    public class SessionManager
    {
        ISessionFactory _sessionfactory;
        public SessionManager()
        {
            _sessionfactory = GetSessionFactory();
        }

        public ISessionFactory GetSessionFactory()
        {
            Configuration ctg = new Configuration().Configure("./NHibernateMSSQL.config");
            //ctg.AddAssembly("Model");
            //return (new Configuration()).Configure().BuildSessionFactory();
            return ctg.BuildSessionFactory();
        }

        public ISession GetSession()
        {
            return _sessionfactory.OpenSession();
        }
    }
}

 

     这里的Configuration ctg = new Configuration().Configure("./NHibernateMSSQL.config");的方式在NHibernate的灵活配置 有讲到。
     当然这里的"./NHibernateMSSQL.config"在实际生产中一般不要写到这里来,一般是从配置文件中读取过来。这里为了方便,我就直接写在这里了。
     这里我也没有去关心这个线程安全不安全的问题了。见谅!

SessionManager.cs类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NHibernate;
using Model.Entities;
//using NHibernate.Cfg;

namespace DAL
{
    public class GetStudent
    {
        private ISession _session;

        public ISession Session
        {
            set { _session = value; }
        }

        public GetStudent(ISession session)
        {
            _session = session;
        }

        public Student GetStudentById(int studentId)
        {
            return _session.Get<Student>(studentId);
        }
    }
}

 

    
Form.cs 代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using DAL;
using NHibernate;
using Model.Entities;
namespace NHibernateTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private ISession _session;
        private SessionManager _helper = new SessionManager();
        private GetStudent _getStudent;
        private void Form1_Load(object sender, EventArgs e)
        {
            _session = _helper.GetSession();
            _getStudent = new GetStudent(_session);

            Student student = _getStudent.GetStudentById(1);
            this.textBox1.Text = student.StudentName;
            this.textBox2.Text = student.StudentPassword;
        }
    }
}

 

OK,开始执行。
叮!第一个错误跳出来:
“NHibernate.Cfg.Configuration”的类型初始值设定项引发异常。
查看详细信息:
未能加载文件或程序集“log4net, Version=1.2.10.0, Culture=neutral, PublicKeyToken=1b44e1d426115821”或它的某一个依赖项。
系统找不到指定的文件。

第一次想到在DAL中加入log4net.dll(这个dll在下载过来的最新版的NHibernate的bin文件夹下有的)。没想到问题依然存在,那好,就先撤销在DAL中的log4net,然后直接到Winform下添加log4net结果错误消失了。但是第二个错误 咚!又出来了!


The following types may not be used as proxies:

Model.Entities.Course:method get_IsDeleted should be 'public/protected virtual' or 'protected internal virtual'

Model.Entities.Course: method set_IsDeleted should be 'public/protected virtual' or 'protected internal virtual'

Model.Entities.Course: method get_IsChanged should be 'public/protected virtual' or 'protected internal virtual'

Model.Entities.Course: method set_IsChanged should be 'public/protected virtual' or 'protected internal virtual'

Model.Entities.Course: method Clone should be 'public/protected virtual' or 'protected internal virtual'

Model.Entities.StudentCourse: method get_IsDeleted should be 'public/protected virtual' or 'protected internal virtual'

Model.Entities.StudentCourse: method set_IsDeleted should be 'public/protected virtual' or 'protected internal virtual'


Model.Entities.StudentCourse: method get_IsChanged should be 'public/protected virtual' or 'protected internal virtual'

Model.Entities.StudentCourse: method set_IsChanged should be 'public/protected virtual' or 'protected internal virtual'

Model.Entities.StudentCourse: method Clone should be 'public/protected virtual' or 'protected internal virtual'

Model.Entities.Student: method get_IsDeleted should be 'public/protected virtual' or 'protected internal virtual'

Model.Entities.Student: method set_IsDeleted should be 'public/protected virtual' or 'protected internal virtual'

Model.Entities.Student: method get_IsChanged should be 'public/protected virtual' or 'protected internal virtual'

Model.Entities.Student: method set_IsChanged should be 'public/protected virtual' or 'protected internal virtual'

Model.Entities.Student: method Clone should be 'public/protected virtual' or 'protected internal virtual'


    这个就是在老赵今天发的我对NHibernate的感受(2):何必到处都virtual的提到的到处都是Virtual了,在MyGeneration生成的映射文件中,有些方法并不是Virtural的,所以会出现编译通过,但是执行出错的情况。
   解决方案,当然是在每个方法和属性上面加上Virtual了。

处理完这个错误,当然还是没有那么顺利就过关啦,第三个错误依旧报来!
我的NHibernate曲折之行【转】
这也就是需要注意的另外一点:在2.1版本中的NHibernate中需要在NHibernate配置文件中加入
<property name='proxyfactory.factory_class'>NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>
这样一节,这个来源于YJingLee博客里的一个答复。

加入以后,继续编译,执行。叮叮!!又来一错误:

“NHibernate.ByteCode.Castle.ProxyFactory”的类型初始值设定项引发异常。
查看详细信息:
未能加载文件或程序集“Castle.DynamicProxy2, Version=2.1.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc”或它的某一个依赖项。
系统找不到指定的文件。
当然应该也是2.1的版本的缘故咯。然后继续到源码文件夹下的bin目录下找到这个DLL,添加。具体信息可以参考这里
继续编译执行。继续出错:

未能加载文件或程序集“Castle.Core, Version=1.0.3.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc”或它的某一个依赖项。系统找不到指定的文件。

神呐,继续找问题,继续添加引用。
继续编译。现在发现在Winform下的引用已经很多了。都是NHibernate2.1执行的依赖项。
全部添加完,相变的引用如下:
我的NHibernate曲折之行【转】
这些引用都可以在NHibernate源码的bin目录下可以找到。
接下来编译。测试通过!!!
执行的界面如下:
我的NHibernate曲折之行【转】

数据库执行脚本下载项目源代码下载

相关文章: