我对NHibernate的理解

昨天使用了NHibernate,总结如下:

首先建一个实体类,类的属性全部要为virtual(只要你用NHibernate,所有public,protected都必须为virtual,否则就会报错)。

然后建一个实体名.hbm.xml的文件(实体名一定要对应),文件内容如下(本人由章松山亲笔写作,严禁复制及转载,违者追究法律责任。):

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
 <class name="Test.Model.Person, Test.Model" table="t_Person">Test.Model.Person是实体类,Test.Model是实体类的命名空间,t_Person是表名

单主键如下:
  <id name="Id" type="Int32" unsaved-value="null">id适合单主键(本人由章松山亲笔写作,严禁复制及转载,违者追究法律责任。)
   <column name="t_Id" length="4" sql-type="int" not-null="true" unique="true" index="PK_Person"/>
   <generator class="assigned" />assigned表示值由程序分配,native则是自增列,当然要与数据表相匹配。
  </id>

当是多主键时,配置如下:

<composite-id class="Test.Model.主键类, 主键类" name = "Person类中的主键类名">
          <key-property name="主键类的属性1" type="String(10)" column="主键1" />
          <key-property name="主键类的属性2" type="String(10)" column="主键2" />
</composite-id>
以下是非主键的属性(本人由章松山亲笔写作,严禁复制及转载,违者追究法律责任。):
  <property name="Name" type="String">
   <column name="t_Name" length="50" sql-type="varchar" not-null="true"/>
  </property>
 </class>
</hibernate-mapping>

NHibernate的配置

<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>数据库连接字符串,App.Config将会有对应的配置
        <property name="connection.isolation">ReadCommitted</property>
        <property name='proxyfactory.factory_class'>NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property>

        NHibernate.ByteCode.Castle要将对应的dll引用进来。

        <!-- Mapping Files -->
        <!--<mapping assembly="Test.Model"/>-->这里如果写了,程序中就不需要加了。
    </session-factory>
</hibernate-configuration>

需要引用的dll如下:

NHibernate

NHibernate.ByteCode.Castle

log4net

Iesi.Collections

Castle.Core

App.Config的配置

<connectionStrings>
    <!--<add name="Connection String" connectionString="Data Source=hzxl;User Id=hzxl;Password=hzxl;Persist Security Info=true" providerName="System.Data.OracleClient"/>-->
    <add name="Connection String" connectionString="Data Source=B2BSQLSERVER;Database=TestOnly;uid=sa;pwd=sa" providerName="System.Data.SqlClient"/>
  </connectionStrings>

ISession session = null;
ISessionFactory factory = null;
ITransaction trans = null;

初始化: 

Configuration ctg = new Configuration().Configure("./NHibernateMSSQL.config");
 ctg.AddAssembly("Test.Model");配置文件中如果有就不需要加这句了。
 factory = ctg.BuildSessionFactory();
 session = factory.OpenSession();

添加:

trans = session.BeginTransaction();
 //使用NHibernate的现有API
 //体验过程。。。。
 try
{
     //对象的实例化方法
     Person p = new Person();
     p.Id = int.Parse(this.txtID.Text);
     p.Name = this.txtName.Text;
     p.CreateDate = DateTime.Now;
     //将对象保存到数据库
     //将对象p必须要转化成数据库能识别的SQL语句
     //由于在ISessionFactory已经保存了所有的OR映射
     //ISession能根据相应的方言来实现SQL语句
                
        session.Save(p);
        trans.Commit();
    }
     catch (Exception)
     {
                trans.Rollback();
     }

查找:

Person p = (Person)session.Get(typeof(Person), int.Parse(this.txtID.Text));

更新:

 //开启事务
 trans = session.BeginTransaction();
 try
 {
         //根据提供的ID找到该对象
         Person p = (Person)session.Get(typeof(Person), int.Parse(this.txtID.Text));
         //修改对象的属性
          p.Name = this.txtName.Text;
          //将修改反映到数据库
          session.Update(p);
          trans.Commit();
 }
 catch (Exception)
  {
                trans.Rollback();
  }

删除:

//开启事务
trans = session.BeginTransaction();
try
{
       //
       //根据提供的ID找到该对象
        Person p = (Person)session.Get(typeof(Person), int.Parse(this.txtID.Text));
       //对象删除
         session.Delete(p);
         rans.Commit();
}
catch (Exception)
{
       trans.Rollback();
}

原文地址:https://www.cnblogs.com/zhangsongshan/p/NHibernate.html