商讯信箱
用户名: @
密  码:   注册|忘记密码
登录
个人用户经销商
您的位置:首页 > 技术频道 > 正文

NHibernate简介 - 什么是NHibernate

session = factory.OpenSession();
User joeCool = (User)session.Load(typeof(User), "joe_cool");
 
这样你又会得到这个对象,设置一下对象的属性,它会在下一次Flush()方法出现的时候被持久化到数据库.
 
// set Joe Cool's Last Login property
joeCool.LastLogon = DateTime.Now;
 
// flush the changes from the Session to the Database
session.Flush();
 
让NH去写入你对对象作出的修改,你只需要Flush Session就可以了.
 
更好的是,你可以从数据库中查询到一个System.Collections.IList:
 
IList userList = session.CreateCriteria(typeof(User)).List();
foreach(User user in userList)
{
       Console.WriteLine(user.Id + " last logged in at " + user.LastLogon);
}
 
这个查询会返回整个表的内容.尤其是当你想要更多的控制时候--像类出所有在March 14, 2004 10:00 PM之后登陆过的用户,你可以:
 
IList recentUsers = session.CreateCriteria(typeof(User)).Add(Expression.Gt("LastLogon", new DateTime(2004, 03, 14, 20, 0, 0))).List();
foreach(User user in recentUsers)
{
       Console.WriteLine(user.Id + " last logged in at " + user.LastLogon);
}
 
文档里还有很多的查询选项,但是以上这些足够让你看出Hinernate的力量了.
不要忘记了,最后要关掉你的Session.
 
// tell NHibernate to close this Session
session.Close();
1 2 3 4 5
【内容导航】
第1页: 第1页 第2页: 第2页
第3页: 第3页 第4页: 第4页
第5页: 第5页
©版权所有。未经许可,不得转载。
[责任编辑:晓徐]
[an error occurred while processing this directive]
[an error occurred while processing this directive]