NHibernate第一个成功的例子
namespace NHibernate.Demo.QuickStart
文件列表:
app.config
User.cs
User.hbm.xml
=====app.config===========================
<?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=127.0.0.1;initial catalog=nhibernate;User id=sa;password=mnm;"
/>
</nhibernate>
</configuration>
====User.hbm.xml=================================================
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<class name="NHibernate.Demo.QuickStart.User, NHibernate.Demo.QuickStart" table="users">
<id name="Id" column="LogonId" type="String(20)">
<generator class="assigned" />
</id>
<property name="UserName" column="Name" type="String(40)"/>
<property name="Password" type="String(20)"/>
<property name="EmailAddress" type="String(40)"/>
<property name="LastLogon" type="DateTime"/>
</class>
</hibernate-mapping>
====User.cs====================================================
using System;
namespace NHibernate.Demo.QuickStart
{
public class User
{
private string id;
private string userName;
private string password;
private string emailAddress;
private DateTime lastLogon;
public User()
{
}
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; }
}
}
}
=========调用============================================
//新增记录
Configuration cfg = new Configuration();
cfg.AddAssembly("NHibernate.Demo.QuickStart");
cfg .AddXmlFile ("User.hbm.xml"); //这里我改了范例中的,使用范例中的写法不成功,不知道为啥
//cfg.AddClass (typeof(User));
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 = "joe@cool.com";
newUser.LastLogon = DateTime.Now;
// 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();
======查询代码=============================
Configuration mCfg=new Configuration();
mCfg.AddXmlFile ("User.hbm.xml");
ISession vSession= mCfg.BuildSessionFactory().OpenSession();
ITransaction vTransaction = vSession.BeginTransaction();
try
{
IList vList=vSession.Find("from NHibernate.Demo.QuickStart.User");//HQL语句实现数据查询
User user = (User)vList[0];
string id = user.Id;
vTransaction.Commit();
}
catch(Exception ex)
{
vTransaction.Rollback();
}
finally
{
vSession.Close();
}
============================================================
工程引用了
HashCodeProvider
log4net
NHibernate
总结:没有准确的提示信息,找不到是什么错误~!