我在那个官网下了最新的版本。
建立一个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()
建立一个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()
参考官方的文档:
http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/quickstart.html
似乎并不相同
namespace Nhibernate
{
class User
{
<class name="NHibernate.User, NHibernate" table="users">...这样有问题吗,不是先是全类名,然后是命名控件的名字吗。数据库是Nhibernate表是users啊难道是因为起的名字和Nhibernate冲突吗??
你全类的名是Nhibernate.User而不是NHibernate.User
感觉上就是装配件,这块算是比较高级,用到了反射!他和正常的 private statid int _id
有什么好处么?
唉,csdn不能上传附件啊。要是能上传附件,我真想吧我那个实例发上去,大家看看啊。
郁闷。
这一句是否也改了呢?
我原来以为是User类不是public方法,可是加上public后,还是原来的错误
还需引用Iesi.CollectionsDLL
nhibernate-mapping.xsd这个文件和映射文件放同一目录<class name="映射类所在命名空间.映射类名, 映射类所在的程序集名" table="映射的数据库表">最近也在看这个东西,写了一个DEMO,想交流的话,留下你的EMAIL
[email protected]
能不能给我发一份你做的几个简单的入门列子啊。你说的程序集名字难道和命名空间不是同一个名字吗?
cfg.AddAssembly("NHibernate");
这里填写的是项目所在的命名空间。是项目的命名空间,还是实体类所在的命名空间,通常他们是一样的吧。如果不使用分层的情况下
应该改成
using System.Collections;