Nhibernate的问题,请哪位大虾帮忙????
在10。1时我无意间发现一个不错的OpenSource的架构Nhibernate,我研究了一天,并且我现在按照http://nhibernate.sourceforge.net/quickstart.html网站的步骤做了实验,但不知为什么总是编译错误,请大家帮忙。
一、代码如下
1.数据库的建立
use NHibernate
go
CREATE TABLE users (
LogonID varchar(20) NOT NULL default '0',
Name varchar(40) default NULL,
Password varchar(20) default NULL,
EmailAddress varchar(40) default NULL,
PRIMARY KEY (LogonID)
)
go
2.创建实体层
using System;
namespace MyNhibernate.Bean
{
public class MyUser
{
private string id;
private string userName;
private string password;
private string emailAddress;
public MyUser()
{
}
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; }
}
}
}
3.创建XML文件(映射数据)
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="MyNhibernate.Bean.MyUser, MyNhibernate.Bean" table="users">
<property name="Id" column= "LogonId" type="String" length="20"/>
<property name="UserName" column= "Name" type="String" length="40"/>
<property name="Password" type="String" length="20"/>
<property name="EmailAddress" type="String" length="40"/>
</class>
</hibernate-mapping>
4.在配置文件中添加代码
在<system.web>上面添加
<configSections>
<section name="nhibernate" type="System.Configuration.NameValueSectionHandler, System, Version=1.0.3300.0,Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
在</system.web>下面添加
<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=jinqi;initial catalog=nhibernate;User id=sa;password=;"
/>
</nhibernate>
5.创建实现代码
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using NHibernate;
using NHibernate.Cfg;
using MyNhibernate.Bean;
using NUnit.Framework;
using NHibernate.Expression;
namespace MyNhibernate.NHibernateWeb
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
Configuration cfg = new Configuration();
cfg.AddAssembly("MyNhibernate");
ISessionFactory factory = cfg.BuildSessionFactory();
ISession session = factory.OpenSession();
ITransaction transaction = session.BeginTransaction();
MyUser newUser =new MyUser();
newUser.Id = "joe_cool";
newUser.UserName = "Joseph Cool";
newUser.Password = "abc123";
newUser.EmailAddress = "joe@cool.com";
// Tell NHibernate that this object should be saved
session.Save(newUser); //错误地方老是错误
// commit all of the changes to the DB and close the ISession
transaction.Commit();
session.Close();
// open another session to retrieve the just inserted user
session = factory.OpenSession();
MyUser joeCool = (MyUser)session.Load(typeof(MyUser), "joe_cool");
// set Joe Cool's Last Login property
joeCool.LastLogon = DateTime.Now;
// flush the changes from the Session to the Database
session.Flush();
IList recentUsers = session.CreateCriteria(typeof(MyUser))
.Add(Expression.Gt("LastLogon", new DateTime(2004, 03, 14, 20, 0, 0)))
.List();
foreach(MyUser user in recentUsers)
{
Assert.IsTrue(user.LastLogon > (new DateTime(2004, 03, 14, 20, 0, 0)) );
}
session.Close();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
二、编译错误如下
“/MyNhibernate”应用程序中的服务器错误。
--------------------------------------------------------------------------------
No persisters for: MyNhibernate.Bean.MyUser
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: NHibernate.MappingException: No persisters for: MyNhibernate.Bean.MyUser
源错误:
行 38:
行 39: // Tell NHibernate that this object should be saved
行 40: session.Save(newUser);
行 41:
行 42: // commit all of the changes to the DB and close the ISession
源文件: d:\inetpub\wwwroot\mynhibernate\nhibernateweb\webform1.aspx.cs 行: 40
堆栈跟踪:
[MappingException: No persisters for: MyNhibernate.Bean.MyUser]
NHibernate.Impl.SessionFactoryImpl.GetPersister(Type theClass)
NHibernate.Impl.SessionImpl.GetPersister(Type theClass)
NHibernate.Impl.SessionImpl.GetPersister(Object obj)
NHibernate.Impl.SessionImpl.Save(Object obj)
[ADOException: Could not save object]
NHibernate.Impl.SessionImpl.Save(Object obj)
MyNhibernate.NHibernateWeb.WebForm1.Page_Load(Object sender, EventArgs e) in d:\inetpub\wwwroot\mynhibernate\nhibernateweb\webform1.aspx.cs:40
System.Web.UI.Control.OnLoad(EventArgs e)
System.Web.UI.Control.LoadRecursive()
System.Web.UI.Page.ProcessRequestMain()
--------------------------------------------------------------------------------
版本信息: Microsoft .NET Framework 版本:1.1.4322.573; ASP.NET 版本:1.1.4322.573