Nhibernate 注意总结

1.持久化类(Customer)和映射xml(Customer.hbm.xml)

如果持久化类的namespace Entities那么xml生成时的路径要为bin\Debug\Customer.hbm.xml。

2.Customer.hbm.xml属性 -->生成操作为“嵌入的资源”;----->复制到输出目录为“始终复制”。

<?xml version="1.0" encoding="utf-8" ?>
<!--映射Customer的xml 设置为嵌入资源-->
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" default-lazy="false">
  <!-- 命名空间 程序集名-->
  <class name="Entities.Customer,Entities" table="customer">
    <!--类名 表名-->
    <id name="Id" unsaved-value="null">
      <!--专门放置主键-->
      <column name="id" length="4" sql-type="Int32" not-null="true" unique="true" />
      <generator class="native" />
    </id>
    <property name="FirstName" column="firstname" type="string" length="50"  />
    <!-- 字段名 -->
    <property name="LastName" column="lastname" type="string" length="50" />
  </class>
</hibernate-mapping>

3.持久化类(Customer)要设置为[Serializable],属性要为virtual。

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

namespace Entities
{
    [Serializable]//可序列化
    public class Customer
    {
        //NHibernate对属性使用的类型不加任何限制。所有的.NET类型和原始类型(比如string,char和DateTime)都可以被映射,
        //也包括.Net 集合(System.Collections)中的类。你可以把它们映射成为值,值集合,或者与其他实体类相关联。Id是一个特殊的属性,
        //代表了这个类的数据库标识符(主键),对于类似于Cat这样的实体类我们强烈建议使用。NHibernate也可以使用内部标识符,
        //但这样我们会失去一些程序架构方面的灵活性。
        //持久化类不需要实现什么特别的接口,也不需要从一个特别的持久化根类继承下来。NHibernate也不需要使用任何编译期处理,
        //比如IL操作,它独立的使用.Net反射机制和运行时类增强(通过Castle.DynamicProxy2 library)。
        //所以不依赖于NHibernate(POJO的类不需要依赖NHibernate),我们就可以把POJO的类映射成为数据库表。
        //为了让上面提到运行时类增强功能生效,NHibernate持久化类的所有的public的属性必须声明为virtual。
        
        private int _id;
        //为了使持久化类可被映射 public virtual
        public virtual int Id
        {
            get { return _id; }
            set { _id = value; }
        }
        private string _firstName;

        public virtual string FirstName
        {
            get { return _firstName; }
            set { _firstName = value; }
        }
        private string _lastName;

        public virtual string LastName
        {
            get { return _lastName; }
            set { _lastName = value; }
        }
    }
}

4.nhibernate2.2的hibernate.cfg.xml配置文件

<?xml version="1.0" encoding="utf-8"?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <!-- properties -->
    <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
    <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
    <property name="connection.connection_string">Data Source=.;Initial Catalog=mytemp;Integrated Security=True</property>
    <property name="show_sql">true</property>
    <property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
    <property name="use_outer_join">true</property>
    <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
    <property name="proxyfactory.factory_class">NHibernate.ByteCode.Castle.ProxyFactoryFactory,NHibernate.ByteCode.Castle</property>
    <!-- mapping assembly -->
    <mapping assembly="Entities" />
  </session-factory>
 </hibernate-configuration>

//配置Configuration
            Configuration cfg = new Configuration().Configure(
                Assembly.GetExecutingAssembly(), //当前代码正从中运行的程序集。
                TestHelper.NAMEPLACENAME + ".hibernate.cfg.xml");
            //测试
            TestHelper.CheckConfiguration(cfg);

hibernateA.cfg.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    省略
    <!-- mapping files -->
    <mapping file="Customer.hbm.xml"/>
  </session-factory>
</hibernate-configuration>

//配置Configuration
            Configuration cfg = new Configuration().Configure(
                Assembly.GetExecutingAssembly(), //当前代码正从中运行的程序集。
                TestHelper.NAMEPLACENAME + ".hibernateA.cfg.xml");
            //测试
TestHelper.CheckConfiguration(cfg);


hibernateB.cfg.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    省略
    <!-- 映射文件 -->
    <!--程序将加载指定的文件 Entities.Customer文件夹下Customer.hbm.xml-->
    <!--这种写法将加载资源文件-->
    <mapping resource="Entities.Customer.hbm.xml" assembly="Entities" />
  </session-factory>
</hibernate-configuration>

//配置Configuration
            //Resource not found: Entities.Customer.hbm.xml
            Configuration cfg = new Configuration().Configure(
                Assembly.GetExecutingAssembly(), //当前代码正从中运行的程序集。
                TestHelper.NAMEPLACENAME + ".hibernateB.cfg.xml");
            //测试
 TestHelper.CheckConfiguration(cfg);

hibernateC.cfg.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    省略
    <!--没有加载映射文件-->
  </session-factory>
</hibernate-configuration>

//配置Configuration
            Configuration cfg = new Configuration().Configure(
                Assembly.GetExecutingAssembly(), //当前代码正从中运行的程序集。
                TestHelper.NAMEPLACENAME + ".hibernateC.cfg.xml");
            //加载程序集中所有映射文件
            //cfg.AddAssembly(Assembly.GetExecutingAssembly());//Could not compile the mapping document: nhibernateDemo.bin.Release.Customer.hbm.xml
cfg.AddAssembly("Entities");

或者

cfg.AddClass(typeof(Entities.Customer));//Resource not found: Entities.Customer.hbm.xml
            //将customer的xml文件放到customer类的上级目录,或是将customer的命名空间加上文件加的名称,这样将不会报上边的错误

或者

 //加载和类对应的映射文件
            cfg.AddFile("Customer.hbm.xml");
            //测试
     TestHelper.CheckConfiguration(cfg);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值