花了大概将近三个小时,在配置文件间来回的折腾,终于人品爆发,老天让我感动了,终于成功了,先将我的遇到的种种挫折和弯路给大家说说,让你们少走弯路【我太阳,NHibernate比Hibernate麻烦多了,还是MyEclipse配起来简单,当初我第一次配Hibernate花了十分钟,汗~】,废话不多说,放代码

首先,数据库
USE Test
CREATE TABLE [dbo].[Cat](
  [CatId] [ char](32) COLLATE Chinese_PRC_CI_AS NOT NULL,
  [ Name] [ nvarchar](16) COLLATE Chinese_PRC_CI_AS NOT NULL,
  [Sex] [ nchar](1) COLLATE Chinese_PRC_CI_AS NULL,
  [Weight] [ real] NULL,
CONSTRAINT [PK_Cat] PRIMARY KEY CLUSTERED
(
  [CatId] ASC
) WITH (IGNORE_DUP_KEY = OFF) ON [ PRIMARY]
) ON [ PRIMARY]

GO

我用的NHibernate版本是2.1.2GA  下载地址 http://down.ddvip.com/view/119296618916063.html
首先新建个ClassLibirary工程,取名NHibernate.Domain,然后添加引用
NHibernate.dll,NHibernate.ByteCode.Castle.dll【在Required_For_LazyLoading\Castle目录下】

新建个类Cat.cs文件
InBlock.gif using System;
InBlock.gif using System.Collections.Generic;
InBlock.gif using System.Linq;
InBlock.gif using System.Text;
InBlock.gif
namespace NHibernate.Domain
InBlock.gif{
InBlock.gif         public class Cat
InBlock.gif        {
InBlock.gif                 private string id;
InBlock.gif                 private string name;
InBlock.gif                 private char sex;
InBlock.gif                 private float weight;
InBlock.gif
                 public Cat()
InBlock.gif                {
InBlock.gif                }
InBlock.gif
                 public virtual string Id
InBlock.gif                {
InBlock.gif                        get { return id; }
InBlock.gif                        set { id = value; }
InBlock.gif                }
InBlock.gif
                 public virtual string Name
InBlock.gif                {
InBlock.gif                        get { return name; }
InBlock.gif                        set { name = value; }
InBlock.gif                }
InBlock.gif
                 public virtual char Sex
InBlock.gif                {
InBlock.gif                        get { return sex; }
InBlock.gif                        set { sex = value; }
InBlock.gif                }
InBlock.gif
                 public virtual float Weight
InBlock.gif                {
InBlock.gif                        get { return weight; }
InBlock.gif                        set { weight = value; }
InBlock.gif                }
InBlock.gif        }
InBlock.gif}

然后添加Cat.cs的映射文件Cat.hbm.xml【Nhibernate数据库配置文件】

<? xml version ="1.0" encoding ="utf-8" ?>
< hibernate-mapping xmlns ="urn:nhibernate-mapping-2.2" namespace ="NHibernate.Domain" assembly ="NHibernate.Domain" >

     < class name ="Cat" table ="Cat" >

        <!-- A 32 hex character is our surrogate key. It's automatically
                        generated by NHibernate with the UUID pattern.
-->
         < id name ="Id" >
             < column name ="CatId" sql-type ="char(32)" not-null ="true" />
             < generator class ="uuid.hex" />
         </ id >

        <!-- A cat has to have a name, but it shouldn' be too long. -->
         < property name ="Name" >
             < column name ="Name" length ="16" not-null ="true" />
         </ property >
         < property name ="Sex" />
         < property name ="Weight" />
     </ class >

</ hibernate-mapping >
注意:
记得Cat.hbm.xml文件右键属性中的Bulid Action选择Embedded Resource
在上述项目中添加个控制台程序取名Test,添加引用NHibernate.dll,NHibernate.ByteCode.Castle.dll
然后再项目中添加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 ="connection.driver_class" >NHibernate.Driver.SqlClientDriver </ property >
         < property name ="connection.connection_string" >Data Source=VISIONSKY;Initial Catalog=Test;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 ="NHibernate.Domain" />



     </ session-factory >

</ hibernate-configuration >




注意:必须保证  xmlns="urn:nhibernate-configuration-2.2" 跟映射文件一致,否则会报错
另外  hibernate.cfg.xml 右键属性copy to output directory这一项选择Copy always

最后在Program.cs文件中添加如下代码
InBlock.gif using System;
InBlock.gif using System.Collections.Generic;
InBlock.gif using System.Linq;
InBlock.gif using System.Text;
InBlock.gif using NHibernate.Cfg;
InBlock.gif using NHibernate;
InBlock.gif using NHibernate.Domain;
InBlock.gif
namespace Test
InBlock.gif{
InBlock.gif         class Program
InBlock.gif        {
InBlock.gif                 static void Main( string[] args)
InBlock.gif                {
InBlock.gif                                ISessionFactory factory     = new Configuration().Configure().BuildSessionFactory();
InBlock.gif                        
InBlock.gif                         ISession session = factory.OpenSession();
InBlock.gif  
InBlock.gif                         ITransaction tx = session.BeginTransaction();
InBlock.gif  
InBlock.gif                         Cat princess = new Cat();
InBlock.gif                         princess.Name = "Princess";
InBlock.gif                         princess.Sex = 'F';
InBlock.gif                         princess.Weight = 7.4f;
InBlock.gif  
InBlock.gif                         session.Save(princess);
InBlock.gif                         tx.Commit();
InBlock.gif  
InBlock.gif                         session.Close();
InBlock.gif                        
InBlock.gif                }
InBlock.gif        }
InBlock.gif}

然后点运行,OK,成功~~~!