15.8. 表达式
在where子句中允许使用的表达式包括 大多数你可以在SQL使用的表达式种类:
数学运算符+, -, *, /
二进制比较运算符=, >=, <=, <>, !=, like
逻辑运算符and, or, not
in, not in, between, is null, is not null, is empty, is not empty, member of and not member of
"简单的" case, case ... when ... then ... else ... end,和 "搜索" case, case when ... then ... else ... end
字符串连接符...||... or concat(...,...)
current_date(), current_time(), current_timestamp()
second(...), minute(...), hour(...), day(...), month(...), year(...),
EJB-QL 3.0定义的任何函数或操作:substring(), trim(), lower(), upper(), length(), locate(), abs(), sqrt(), bit_length()
coalesce() 和 nullif()
cast(... as ...), 其第二个参数是某Hibernate类型的名字,以及extract(... from ...),只要ANSI cast() 和 extract() 被底层数据库支持
任何数据库支持的SQL标量函数,比如sign(), trunc(), rtrim(), sin()
JDBC参数传入 ?
命名参数:name, :start_date,
1
SQL 直接常量 ’foo’, 69, ’1970-01-01 10:00:01.0’
Java public static final 类型的常量 eg.Color.TABBY
关键字in与between可按如下方法使用:
代码内容
from DomesticCat cat where cat.name between ’A’ and ’B’
from DomesticCat cat where cat.name in ( ’Foo’, ’Bar’, ’Baz’ )
而且否定的格式也可以如下书写:
代码内容
from DomesticCat cat where cat.name not between ’A’ and ’B’
from DomesticCat cat where cat.name not in ( ’Foo’, ’Bar’, ’Baz’ )
同样, 子句is null与is not null可以被用来测试空值(null).
在Hibernate配置文件中声明HQL“查询替代(query substitutions)”之后, 布尔表达式(Booleans)可以在其他表达式中轻松的使用:
代码内容
<property name="hibernate.query.substitutions">true 1, false 0</property>
系统将该HQL转换为SQL语句时,该设置表明将用字符 1 和 0 来 取代关键字true 和 false:
代码内容
from Cat cat where cat.alive = true
你可以用特殊属性size, 或是特殊函数size()测试一个集合的大小。
代码内容
from Cat cat where cat.kittens.size > 0
from Cat cat where size(cat.kittens) > 0
对于索引了(有序)的集合,你可以使用minindex 与 maxindex函数来引用到最小与最大的索引序数。 同理,你可以使用minelement 与 maxelement函数来 引用到一个基本数据类型的集合中最小与最大的元素。
代码内容
from Calendar cal where maxelement(cal.holidays) > current date
from Order order where maxindex(order.items) > 100
from Order order where minelement(order.items) > 10000
在传递一个集合的索引集或者是元素集(elements与indices 函数) 或者传递一个子查询的结果的时候,可以使用SQL函数any, some, all, exists, in
代码内容
select mother from Cat as mother, Cat as kit
where kit in elements(foo.kittens)
select p from NameList list, Person p
where 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)
注意,在Hibernate3种,这些结构变量- size, elements, indices, minindex, maxindex, minelement, maxelement - 只能在where子句中使用。
一个被索引过的(有序的)集合的元素(arrays, lists, maps)可以在其他索引中被引用(只能在where子句中):
代码内容
from Order order where order.items[0].id = 1234
select person from Person person, Calendar calendar
where calendar.holidays[’national day’] = person.birthDay
and person.nationality.calendar = calendar
select item from Item item, Order order
where order.items[ order.deliveredItemIndices[0] ] = item and order.id = 11
select item from Item item, Order order
where order.items[ maxindex(order.items) ] = item and order.id = 11
在[]中的表达式甚至可以是一个算数表达式。
select item from Item item, Order order
where order.items[ size(order.items) - 1 ] = item
对于一个一对多的关联(one-to-many association)或是值的集合中的元素, HQL也提供内建的index()函数,
代码内容
select item, index(item) from Order order
join order.items item
where index(item) < 5
如果底层数据库支持标量的SQL函数,它们也可以被使用
from DomesticCat cat where upper(cat.name) like ’FRI%’
如果你还不能对所有的这些深信不疑,想想下面的查询。如果使用SQL,语句长度会增长多少,可读性会下降多少:
代码内容
select cust
from Product prod,
Store store
inner join store.customers cust
where prod.name = ’widget’
and store.location.name in ( ’Melbourne’, ’Sydney’ )
and prod = all elements(cust.currentOrder.lineItems)
提示: 会像如下的语句
SELECT cust.name, cust.address, cust.phone, cust.id, cust.current_order
FROM customers cust,
stores store,
locations loc,
store_customers sc,
product prod
WHERE prod.name = ’widget’
AND store.loc_id = loc.id
AND loc.name IN ( ’Melbourne’, ’Sydney’ )
AND sc.store_id = store.id
AND sc.cust_id = cust.id
AND prod.id = ALL(
SELECT item.prod_id
FROM line_items item, orders o
WHERE item.order_id = o.id
AND cust.current_order = o.id
)
分享到:
相关推荐
HQL查询语言基础,HQL查询语言基础,HQL查询语言基础
HQL查询语言基础.
HQL 查询语言基础 一
HQL 查询语言基础 二
HQL查询语言基础 标准写法
HQL查询语言基础知识 + 插入,查询,更新, 代码集. HQL查询语言基础知识 + 插入,查询,更新, 代码集
HQL Hibernate查询语言,HQL的学习和参考的文档,开发必备。
hql语法,内容详细,教程,it人员的又一使用参考值得你拥有
HQL语言详细描述及讲解,在文中结合例子得程序进行了详细的说明,更容易让大家看的明白!!!!
Hibernate查询语言HQL
Hibernate查询语言HQL.PPT
NHibernate 查询语言(HQL,NHibernate QueryLanguage)是NHibernate 特有的基于面向对象 的SQL 查询语言,它具有继承、多态和关联等特性。实际上是用OOP 中的对象和属性映射 了数据库中的表和列。 例如这一句:...
HQL查询是一种面向对象的查询语言,其中没有表和字段的概念,只有类、对象和属性的概念,HQL 是应用较为广泛的方式.这是本人对HQL知识点做的一个总结性的练习
HQL是Hibernate Query Language的缩写,语法很想SQL,但是HQL是一种面向对象的查询语言。SQL的操作对象是数据列、表等数据库对象,而HQL操作的是类、实例、属性
HQL是完全面向对象的查询语言,因此可以支持继承和多态等特征。HQL查询依赖于Query类,每个Query实例对应一个查询对象,使用HQL查询按 如下步骤进行: (1)获取Hibernate Session对象; (2)编写HQL语句; (3)以HQL...
hibernate 查询语言 HQL的使用详解及实例, 个人笔记,适合初学者
Hibernate配备了一种非常强大的查询语言,这种语言看上去很像SQL。但是不要被语法结构 上的相似所迷惑,HQL是非常有意识的被设计为完全面向对象的查询,它可以理解如继承、多态 和关联之类的概念。
HQL中文语法参考, 详细描述hibernate 查询语言的各种语法,是不可多得的HQL中文参考资料!