1 public IList<Customer> GetAllHql() 2 { 3 IList<Customer> result = null; 4 ISession session = _sessionManager.GetSession(); 5 6 try 7 { 8 9 result = session.CreateQuery("from Customer").List<Customer>(); 10 } 11 catch (Exception) 12 { 13 throw; 14 } 15 finally 16 { 17 session.Close(); 18 } 19 20 return result; 21 }
注意:
result = session.CreateQuery("from Customer").List<Customer>();
Customer是类名,而不是数据库表名。
这正是Nhibernate的好处:屏蔽对数据库的底层操作,直接面向对象(实体类)编程。
实例:
1 public IList<Customer> GetAllCustomers_HQL(){ 2 return session.CreateQuery("from Customer c").List<Customer>(); 3 } 4 5 public IList<int> GetCustomerId_HQL() { 6 return session.CreateQuery("select c.CustomerId from Customer c").List<int>(); 7 } 8 9 public IList<Object[]> GetCustomerInfo_HQL() { 10 return session.CreateQuery("select c.FirstName,count(c.FirstName) from Customer c group by c.FirstName").List<Object[]>(); 11 } 12 13 public IList<Object[]> AggregateFunction() 14 { 15 return session.CreateQuery("select avg(c.CustomerId),sum(c.CustomerId),count(c) from Customer c") 16 .List<Object[]>(); 17 } 18 19 public IList<String> Distinct_HQL() { 20 return session.CreateQuery("select distinct c.FirstName from Customer c").List<String>(); 21 } 22 23 public IList<Customer> Where_HQL() { 24 return session.CreateQuery("from Customer c where c.FirstName='aaaa'").List<Customer>(); 25 } 26 27 public IList<Customer> Where_HQL(String firstname) 28 { 29 String hql = String.Format("from Customer c where c.FirstName='{0}'",firstname); 30 return session.CreateQuery(hql).List<Customer>(); 31 } 32 33 public IList<Customer> WhereLike_HQL(String firstname) 34 { 35 String hql = String.Format("from Customer c where c.FirstName like'%{0}%'", firstname); 36 return session.CreateQuery(hql).List<Customer>(); 37 } 38 39 public IList<Customer> WhereLike_HQL2(String firstname) 40 { 41 String hql = "from Customer c where c.FirstName like ?"; 42 return session.CreateQuery(hql).SetString(0,"%"+firstname+"%").List<Customer>(); 43 } 44 45 public IList<Customer> WhereLike_HQL3(String firstname) 46 { 47 String hql = "from Customer c where c.FirstName like :fname"; 48 return session.CreateQuery(hql).SetString("fname", "%" + firstname + "%").List<Customer>(); 49 } 50 51 public IList<Customer> OrderBy_HQL() { 52 return session.CreateQuery("from Customer c order by c.FirstName asc,c.LastName desc").List<Customer>(); 53 } 54 55 //查询下定单的客户信息 56 public IList<Customer> InnerJoin_HQL() 57 { 58 string hql = "select distinct a from Customer a inner join a.Orders b "; 59 return session.CreateQuery(hql).List<Customer>(); 60 } 61 62 //public IList<Customer> UseCriteriaAPI_GetCustomersWithOrders(DateTime orderDate)(){ 63 // return null; 64 //} 65 66 //查询下定单的客户信息() 67 public IList<Customer> InnerJoin_HQL(String orderDate) 68 { 69 string hql = "select distinct a from Customer a inner join a.Orders b where b.OrderDate>:orderDate"; 70 return session.CreateQuery(hql).SetString("orderDate",orderDate).List<Customer>(); 71 }
------------------------------------------------------------------------------
HQL查询返回不规则对象:
/// <summary> /// HQL返回不规则对象 /// </summary> /// <param name="orderDate"></param> /// <returns></returns> public IList<Object[]> GetObjectByHQL(String orderDate) { //Object的成员依次是Customer,OrderDate string hql = "select distinct a,b.OrderDate from Customer a " + "inner join a.Orders b where b.OrderDate=:orderDate"; return _session.CreateQuery(hql) .SetString("orderDate", orderDate) .List<Object[]>(); }
Object的成员依次是Customer,OrderDate,
IList<Object[]> objects = customerService.GetObjectByHQL(order1.OrderDate.ToString()); foreach (var objectse in objects) { Console.WriteLine("{0}---{1}", ((Customer)objectse[0]).CustomerId, objectse[objectse.Length-1].ToString()); }
调用者必须知道Object的成员及其类型,才能正确使用查询结果,这样很不好。
改进的做法是:
创建一个类Object的成员类型,将HQL返回不规则对象转化为通用对象,减低调用着使用的的复杂度
public class HQLQueryResult { public Customer Customer { get; set; } public DateTime OrderDate { get; set; } }
/// <summary> /// 将HQL返回不规则对象转化为通用对象,减低调用着使用的的复杂度 /// </summary> /// <param name="orderDate"></param> /// <returns></returns> public IList<HQLQueryResult> GetObjectByHQLAndTransferToEasyObject(String orderDate) { List<HQLQueryResult> results = new List<HQLQueryResult>(); //Object的成员依次是Customer,OrderDate string hql = "select distinct a,b.OrderDate from Customer a " + "inner join a.Orders b where b.OrderDate=:orderDate"; IList<Object[]> objects =_session.CreateQuery(hql).SetString("orderDate", orderDate).List<Object[]>(); foreach (var objectse in objects) { HQLQueryResult tmp = new HQLQueryResult() { Customer= ((Customer)objectse[0]), OrderDate = (DateTime)objectse[objectse.Length - 1] }; results.Add(tmp); } return results; }
调用者可以这要调用:
IList<HQLQueryResult> objects = customerService.GetObjectByHQLAndTransferToEasyObject(order1.OrderDate.ToString()); foreach (var objectse in objects) { Console.WriteLine("{0}---{1}", objectse.Customer.CustomerId, objectse.OrderDate.ToString()); }