我在那个官网下了最新的版本。
建立一个window应用程序。在“引用”里面添加了Nhibernate.dll
这是类文件:
using System;
using System.Collections.Generic;
using System.Text;namespace Nhibernate
{
    class User
    {        private string id;
        private string userName;
        private string password;
        private string emailAddress;
        private DateTime lastLogon;        public string Id
        {
            get { return id; }
            set { id = value; }
        }        public string UserName
        {
            get { return userName; }
            set { userName = value; }
        }        public string Password
        {
            get { return password; }
            set { password = value; }
        }        public string EmailAddress
        {
            get { return emailAddress; }
            set { emailAddress = value; }
        }        public DateTime LastLogon
        {
            get { return lastLogon; }
            set { lastLogon = value; }
        }    }
}
这是映射文件:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
  <class name="NHibernate.User, NHibernate" table="users">
    <id name="Id" column="LogonId" type="String" length="20">
      <generator class="assigned" />
    </id>
    <property name="UserName" column="Name" type="String" length="40"/>
    <property name="Password" type="String" length="20"/>
    <property name="EmailAddress" type="String" length="40"/>
    <property name="LastLogon" type="DateTime"/>
  </class>
</hibernate-mapping>
这是配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.3300.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
  </configSections>
  <nhibernate>    <add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />    <add key="hibernate.dialect" value="NHibernate.Dialect.MsSql2000Dialect" />    <add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />    <add key="hibernate.connection.connection_string" value="Server=localhost;initial catalog=Nhibernate;User ID=sa;Password=;Min Pool Size=2" />  </nhibernate></configuration>
这是程序运行的代码段:
 Configuration cfg = new Configuration();
            cfg.AddAssembly("NHibernate");
            ISessionFactory factory = cfg.BuildSessionFactory();
            ISession session = factory.OpenSession();
            ITransaction transaction = session.BeginTransaction();
            User newUser = new User();
            newUser.Id = "joe_cool";
            newUser.UserName = "Joseph Cool";
            newUser.Password = "abc123";
            newUser.EmailAddress = "[email protected]";
            newUser.LastLogon = DateTime.Now;            // Tell NHibernate that this object should be saved
            try
            {
                session.Save(newUser);
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.ToString());
            }            // commit all of the changes to the DB and close the ISession
            transaction.Commit();
            session.Close();
就是Save的时候报错:
未处理 NHibernate.MappingException
  Message="Unknown entity class: Nhibernate.User"
  Source="NHibernate"
  StackTrace:
       在 NHibernate.Impl.SessionFactoryImpl.GetEntityPersister(Type theClass)
       在 NHibernate.Impl.SessionImpl.GetClassPersister(Type theClass)
       在 NHibernate.Impl.SessionImpl.GetEntityPersister(Object obj)
       在 NHibernate.Impl.SessionImpl.SaveWithGeneratedIdentifier(Object obj, CascadingAction action, Object anything)
       在 NHibernate.Impl.SessionImpl.Save(Object obj)
       在 Nhibernate.Form1.button1_Click(Object sender, EventArgs e) 位置 E:\WinPro\Nhibernate\Nhibernate\Form1.cs:行号 41
       在 System.Windows.Forms.Control.OnClick(EventArgs e)
       在 System.Windows.Forms.Button.OnClick(EventArgs e)
       在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       在 System.Windows.Forms.Control.WndProc(Message& m)
       在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
       在 System.Windows.Forms.Button.WndProc(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.Run(Form mainForm)
       在 Nhibernate.Program.Main() 位置 E:\WinPro\Nhibernate\Nhibernate\Program.cs:行号 17
       在 System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
       在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       在 System.Threading.ThreadHelper.ThreadStart()

解决方案 »

  1.   

    映射出错。要保证你的映射文件和class文件在一个目录.另外,假设class文件是user.cs,那么映射文件应该是user.hbm.xml.另外,property最好使用virtual的。
    参考官方的文档:
    http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/quickstart.html
      

  2.   

    我的类User.cs和User.hbm.xml是在同一个目录下的,而已那个xml文件也设置了:内嵌的资源
      

  3.   

    在bin里面是不是只有添加Nhibernate这一个dll啊
      

  4.   

    Nhibernate.User 与NHibernate.User
    似乎并不相同
      

  5.   

    <class name="NHibernate.User
    namespace Nhibernate
    {
    class User
    {
      

  6.   

    cfg.AddAssembly("NHibernate");最好 不要用NHibernate的命名空间,随便取一个,TestNHibernate也好啊
      

  7.   

    我的命名空间是 Nhibernate啊,下面的类User
     <class name="NHibernate.User, NHibernate" table="users">...这样有问题吗,不是先是全类名,然后是命名控件的名字吗。数据库是Nhibernate表是users啊难道是因为起的名字和Nhibernate冲突吗??
      

  8.   

    老大"NHibernate.User,这个是类名,NHibernate(命名空间,这个命名空间下有User类吗?).User(类名)
    你全类的名是Nhibernate.User而不是NHibernate.User
      

  9.   

    不知道Nhibernate和传统的写法有什么区别?
    感觉上就是装配件,这块算是比较高级,用到了反射!他和正常的 private statid int _id
    有什么好处么?
      

  10.   

    对不起,我原来的命名空间和映射文件里面的确不一样。我改了以后,还是那错误啊。
    唉,csdn不能上传附件啊。要是能上传附件,我真想吧我那个实例发上去,大家看看啊。
    郁闷。
      

  11.   

    cfg.AddAssembly("NHibernate");
    这一句是否也改了呢?
      

  12.   

    Configuration cfg = new Configuration().Configure();
      

  13.   

    楼上写的什么意思啊??
    我原来以为是User类不是public方法,可是加上public后,还是原来的错误
      

  14.   

    你可以不要看我上面说的
    还需引用Iesi.CollectionsDLL
    nhibernate-mapping.xsd这个文件和映射文件放同一目录<class name="映射类所在命名空间.映射类名, 映射类所在的程序集名" table="映射的数据库表">最近也在看这个东西,写了一个DEMO,想交流的话,留下你的EMAIL
      

  15.   

    谢谢楼上啊。我也是刚刚接触,看了官网的快速指导,调试这个实例没有成功
    [email protected] 
    能不能给我发一份你做的几个简单的入门列子啊。你说的程序集名字难道和命名空间不是同一个名字吗?
    cfg.AddAssembly("NHibernate");
    这里填写的是项目所在的命名空间。是项目的命名空间,还是实体类所在的命名空间,通常他们是一样的吧。如果不使用分层的情况下
      

  16.   

    using System.Collections.Generic;
    应该改成
    using System.Collections;