HQL的使用-part2 hql的查询(3)

 

4.6.1 HQL 查询(3)

6. 表达式

HQL 的功能非常丰富,其where 子句后支持的运算符异常丰富,不仅包括SQL 的运算符,也包括EJB-QL的运算符等。

where子句中允许使用大部分SQL支持的表达式。
数学运算符+,-, *,1 等。

二进制比较运算符=,>=,<=,<>, !=, like 等。

逻辑运算符 and,or,not 等。

in, not in, between, is nul, is not null, is empty, is not empty, member of and notmemberof等。

简单的 case,case ... when... then... else ... end 和 case, casewhen ... then... else .end 等。

字符串连接符 valuel// value2 或者使用字符串连接函数 concat(valuel ,value2)。

时间操作函数:currencdateO,currenCtimeO,currenctimestampO,secondO,minuteO,hourO,dayO,monthO,ye缸o等。

HQL还支持 EJB-QL3.0 所支持的函数或操作: substringO,trimO,10werO,upperO,leng由0,10cateO,absO,sqrtO,biCleng由0, coalesceO 和 nullif()等。

还支持数据库的类型转换函数,如 cast(...as ..小第二个参数是 Hibernate 的类型名,或者 extract(...from ...),前提是底层数据库支持ANSI castO 和 extractO。如果底层数据库支持单行函数:signO,truncO,rtrimO, sin()。则 HQL 语句也完全可以支持。

HQL 语句支持使用"?"作为参数占位符,这与 JDBC 的参数占位符一致,也可使用命名参数占位符号,方法是在参数名前加冒号":",如 :start_date, :xl 等。也可在 where 子句中使用 SQL 常量,如'foo',69, '1970-01-0110:∞:01.0'等。还可以在 HQL 语句中使用 Javapublic static final 类型的常量,如eg.Color.TABBY。

除此之外, where 子句还支持如下的特殊关键字用法。

in 与 between...and 可按如下方法使用:

from DomesticCat cat where cat.name between 
'A' and 'B'from DomesticCat cat
where cat.name in ( 'Faa'.'Bar',Baz')

当然,也支持 notin 和 not between...and 的使用,例如:

from DomesticCat cat where cat.name not between 'A' and 'B'
from DomesticCat cat where cat.name not in ( 'Faa'.'Bar'.'Baz' )

子句 is null 与 isnotnull 可以被用来测试空值,例如:

from DomesticCat cat where cat.name is null;
from Person as p where p.address is not null;

如果在Hibernate配置文件中使用,必须进行如下声明:

 <propertyname="hibernate.query.
substitutions">truei,false O</property>
上面的声明表明:将HQL转换 SQL语句时,应使用字符1 和 O 来取代关键字true和 false。然后才可以在表达式中使用布尔表达式,例如:
from Cat cat where cat.alive = true
size 关键字用于返回一个集合的大小,例如:
from Cat cat wherecat.kittens.size> 
0from Cat cat where size(cat.kittens) > 0
对于有序集合,还可使用 minindex 与 maxindex 函数代表最小与最大的索引序数。同理,可以使用 minelement 与 maxelement 函数代表集合中最小与最大的元素。例如:
from Calendar cal where maxelement(cal.holidays) > 
current datefrom Order order where
maxindex(order.items) > 100from Order order
where minelement(order.items) > 10000
可以使用SQL 函数如any,some,all,exists,in 等来操作集合里的元素,例如:
//操作集合元素
select mother from Cat as mother, Cat as kitwhere kit in elements(foo.kittens)
lip 的 name属性等于集合中某个元素的name属性
select p from NameList list, Person pwhere p.name = some elements(list.names)
//操作集合元素
from Cat cat where exists elements(cat.kittens)from Player p where 3 >
all elements(p.scores)from Show show where 'fizard' in indices(show.acts)

注意,在这些结构变量中:size, elements, indices, minindex, maxindex, minelement,maxelement 等,只能在 where 子句中使用。

在 where 子句中,有序集合的元素(arrays,lists,maps)可以通过[ ]运算符访问。

例如:

Ilitems 是有序集合属性. items[O]代表第一个元素
from Order order where order.items[O] .id=1234
Ilholidays是 map集合属性.holidays[national day]是代表其中一个元素
selectperson fromPerson person, Calendar
calendarwhere calendar.holidays['national day'] =
person.birthDayand person.nationality.calendar = calendar
//下面同时使用list 集合和map集合属性
select itemfrom iternitem, Order orderwhere order.
items[ order.deliveredItemIndices[O] ] = item
and order.id = 11select item from Item item,
Order orderwhere order.items[ maxindex
(order.items) ] = item and order.id = 11

在[ ]中的表达式甚至可以是一个算数表达式,例如:
select item from Item item, Order orderwhere 
order.items[ size(order.items) - 1 ] = item

借助于 HQL,可以大大简化选择语句的书写,提高查询语句的可读性,看下面的HQL语句:
select custfrom Product prod,Store store
innerjoin store.customers custwhere prod.name = 'widget'
and store.location.name in ( 'Melbourne',
'Sydney' )and prod = allelements(cust.currentOrder.lineiterns)
如果翻译成SQL语句,将变成如下形式:
SELECT cust.name,cust.address,cust.phone,
cust.id,cust.current_orderFROM customers cust,
stores store,locations loc,store_customers sc,
product prodWHERE prod.name = 'widget'AND store.
loc id = loc.idAND loc.name IN ( 'Melbourne',
'Sydney' )AND sc.store id = store.idAND sc.cust
id = cust.idAND prod.id = ALL{SELECT item.
prod_idFROM line_items item, orders0
WHERE item.order id = o.idAND cust.current ord
er = o.id
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值