NHibernate HQL相关语法
NHibernate 相关操作
--------创建--------
Person person = new Person();
赋值,操作。
更新:
Person person = (Person)session.Get(typeof(Person), 1);
赋值,更新。
--------读取--------
1.Person person = (Person)session.Get(typeof(Person), 1);
2.子查询
string query="from login a where a.id in (from login b where uname='2222222222222')";
IList list=session.session.CreateQuery(query).List();
3.自查询
string query="from login where id not in (select a.loginID from userinfo as a )";
IList list=session.session.CreateQuery(query).List();
--------删除--------
session.Delete(person);
session.Delete("from login where id in ('1','2','3','4','5')");
session.Delete("from login where id ='5'");
--------回滚--------
trans.Rollback();
语句查询(HQL):
1. 返回所有的会员:
string query = "from Member";
IList member = session.CreateQuery(query).List();
2. 返回以b开头的会员:
string query = "from Member m where m.name like b%";
IList members= session.CreateQuery(query).List();
3. 返回所有的会员ID和名称:
string query = "select m.MemberId, m.Name from Member m";
IList members = session.CreateQuery(query).List();
注意:此处的MemberId, Name均为Member对象的属性.
返回的并不是members对象的集合,而是一个一维数组的集合,
其中array[0]为MemberId, array[1]为Name
4. 返回会员的总数:
string query = "select count(*) from Member";
IEnumerator e = Session.CreateQuery(query).Enumerable().GetEnumerator();
e.MoveNext;
int rowCount = (int)(e.Current);
5. 返回指定(从20-40)的会员
string query = "from Member";
IList Members = session.CreateQuery(query)
.SetFirstResult(20).SetMaxResults(20).List()
6. 返回所有记录
IList userList = session.CreateCriteria( typeof (User)).List();
7 .排序
session = cfg.BuildSessionFactory().OpenSession();
System.Collections.IList list =
session.CreateCriteria(typeof(Entitys.login))
.AddOrder(new NHibernate.Expression.Order("id",false)).List();