HQL(Hibernate Query language)语言|getCurrentSession().createQuery



在 HQL 语言中,HQL 关键字不区分大小写,但是持久类的名称,属性都区分大小写。这个应该很容易理解,NHibernate的关键字如SELECT ,FROM ,ORDER BY ,GROUP BY 等最后都是转化为标准的sql 语句,在sql 语句中,它们是不区分大小写的。但是为什么持久类要区分大小写呢?因为持久类是c#中的类,而且持久类通过mapping xml 文件去mapping 数据库中的数据表。c# 中的类Customer,和CUSTOMER是两个不同的类,原则上他们的可以mapping 到不同的数据表。归根结底是c#中的类是区分大小写的。

 

 

1.From 子句

这是最简单的HQL 语句,查询所有CUSTOMER实例,返回值是CUSTOMER 实例的list。

public IList<CUSTOMER> FetchAllCustomer()

{

return NHibernateHelper.GetCurrentSession().CreateQuery(“from CUSTOMER “).List<CUSTOMER>();

}

当然用AS取别名也是可以的。

public IList<CUSTOMER> FetchAllCustomerByAlias()

{

return NHibernateHelper.GetCurrentSession().CreateQuery(“from CUSTOMER as c “).List<CUSTOMER>();

}

不用AS 也可以直接取别名,这样也是没有问题的。

public IList<CUSTOMER> FetchAllCustomer()

{

return NHibernateHelper.GetCurrentSession().CreateQuery(“from CUSTOMER c “).List<CUSTOMER>();

}

 

2.Select 子句

在指定查询某一对象的一个或多个属性时,可用select 子句,返回值可以是 int list,strign list, object[] list 。新版的NHibernate中的select 子句后必须要指明查询的属性,如果查询全部属性,直接去掉select 关键字,否则会引发Antlr.Runtime.NoViableAltException 异常。本例使用的是NHibernate 2.1

public IList<int> FetchCustomer_SelectTest1()

{

return NHibernateHelper.GetCurrentSession().CreateQuery(“select CustomerID from CUSTOMER “).List<int>();

}

下面的做法是错误的。

public IList< CUSTOMER > FetchCustomer_SelectTest1()

{

return NHibernateHelper.GetCurrentSession().CreateQuery(“select from CUSTOMER “).List< CUSTOMER >();

}

用object[]返回多个属性

public IList<object[]> FetchCustomer_SelectTest2()

{

return NHibernateHelper.GetCurrentSession().CreateQuery(“select FirstName,LastName from CUSTOMER “).List<object[]>();

}

用object[] 也可以返回聚合函数

 

public IList<object[]> FetchCustomer_SelectTest3()

{

 

return NHibernateHelper.GetCurrentSession().CreateQuery(“select count(CustomerID),FirstName from CUSTOMER group by FirstName “).List<object[]>();

 

}

Sql中的count(*)用HQL怎么写?HQL的语法是只需取别名,然后count(别名)就ok。

public IList<object[]> FetchCustomer_SelectTest4()

{

 

return NHibernateHelper.GetCurrentSession().CreateQuery(“select count(c),FirstName from CUSTOMER c group by FirstName “).List<object[]>();

 

}

 

 

 

 

distinct 的用法和 sql 语法相同。

 

public IList<string> FetchCustomer_SelectTest6()

 

{

 

return NHibernateHelper.GetCurrentSession().CreateQuery(” select distinct c.FirstName from CUSTOMER c “).List<string>();

 

}

 

in 的用法和sql 语法相同

 

 

 

public IList<CUSTOMER> FetchCustomer_SelectTest7()

 

{

 

return NHibernateHelper.GetCurrentSession().CreateQuery(” select distinct c from CUSTOMER c where c.FirstName in (‘david’,'Tina’) “).List<CUSTOMER>();

 

}

 

 

 

 

 

3.Where 子句

 

where 子句基本上支持sql 中的所有条件表达式,但是字符串连接符号有所改变,sql 中用“+”,而HQL用||,这个和ORACLE 是一样的。

 

参数的表示方法也有所不同,SQL中用“@”,而HQL是用“:”,这个又是和ORACLE一致的。

 

 

 


public IList<CUSTOMER> FetchCustomer_SelectTest8()

 

{

 

return NHibernateHelper.GetCurrentSession().CreateQuery(” select distinct c from CUSTOMER c where c.FirstName||c.LastName=’davidliu’ “).List<CUSTOMER>();

 

}

 

HQL 参数的用法:

 

 

 

public IList<CUSTOMER> FetchCustomer_SelectTest9()

 

{

 

return NHibernateHelper.GetCurrentSession().CreateQuery(” select distinct c from CUSTOMER c where c.FirstName||c.LastName=:fullName “).SetString(“fullName”, “davidliu”).List<CUSTOMER>();

 

}

 

 

 

 

 

 

 

where 子句允许出现的表达式包括了在SQL 中的大多数情况:

 

· 数学操作符:+, -, *, /

 

· 真假比较操作符:=, >=, <=, <>, !=, like

 

· 逻辑操作符:and, or, not

 

· 字符串连接操作符:||

 

· SQL 标量函数:upper(),lower()

 

· 没有前缀的( ):表示分组

 

· in, between, is null

 

· 位置参数:?

 

· 命名参数::name, :start_date, :x 1

 

· SQL 文字:’foo’, 69, ’1970-01-01 10:00:01.0′

 

· 枚举值或常量:Color.Tabby

 

 

 

4,order by 子句

 

 

 

按实例的某一属性升序ASC ,或降序DESC返回customer 集合。

 

public IList<CUSTOMER> Orderby()

 

{

 

return NHibernateHelper.GetCurrentSession().CreateQuery(” from CUSTOMER c order by c.FirstName asc,c.LastName desc”)

 

.List<CUSTOMER>();

 

}

 

5,group by 子句

 

group by 子句和聚合函数一起使用,除了集合函数之外的所有select 属性都需要group by

 

 

 

 

 

 

public IList<object[]> GroupBy()

 

{

 

return NHibernateHelper.GetCurrentSession().CreateQuery(“select count(c),FirstName from CUSTOMER c group by c.FirstName “)

 

.List<object[]>();

 

}

 

 

HQL(Hibernate Query language)语言|getCurrentSession().createQuery.