Hibernate查询语言HQL 八大要点

开发 后端
本文讲述Hibernate查询语言HQL。Hibernate拥有一种功能非常强大的查询语言,这种语言被有意得与SQL非常相似,便于开发人员掌握。但不要被HQL的语法表面所迷惑,HQL完全是面向对象的,可以用来过程多态、继承、关联等关系。

Hibernate拥有一种功能非常强大的查询语言,这种语言被有意得与SQL非常相似,便于开发人员掌握。但不要被HQL的语法表面所迷惑,HQL完全是面向对象的,可以用来过程多态、继承、关联等关系。

1大小写敏感(Case Sensitivity)

HQL中的使用的Java的类名和属性名是大小写敏感的,其他的关键字都是大小写不敏感的。所以“SeLeCT”等同与“sELEct”,也等同于“SELECT”,因为它不是Java类名,也不是Java类的属性名。但Java类net.sf.hibernate.eg.FOO不等同于net.sf.hibernate.eg.Foo,同样foo.barSet也不等同于foo.BARSET。

在本手册中,HQL中的关键字均采用小写,一些用户可能发现HQL的关键字采用大写更易读,但我们也发现,当把这些HQL嵌入Java代码中,看起来很丑陋。

2from从句(The from clause)

Hibernate中最简单的from查询可能是:

  1. from eg.Cat 

只是简单的返回eg.Cat类的所有实例。

在很多时候你可能需要为类设置别名(alias),因为你可能需要在查询的其他部分引用Cat。

  1. from eg.Cat as cat 

关键字as是可选的,我们也可以写成:

  1. from eg.Cat cat 

可以出现多个类,然后返回一个“笛卡儿积”或交叉连接:

  1. from Formula as form, Parameter as param 

HQL中的别名用小写字母是一个好习惯,符合Java本地变量的命名规范。

3关联和连接(Associations and joins)

我们使用别名关联实体、甚至用join来关联值的集合的元素。

  1. from eg.Cat as cat  
  2.  
  3.     inner join cat.mate as mate  
  4.  
  5.     left outer join cat.kittens as kitten  
  6.  
  7. from eg.Cat as cat left join cat.mate.kittens as kittens  
  8.  
  9. from Formula form full join form.parameter param  

支持的连接类型借鉴自ANSI SQL:

· inner join

·  left outer join

·  right outer join

· full join (不常用)

inner join, left outer join和right outer join可以简写。

  1. from eg.Cat as cat  
  2.  
  3.     join cat.mate as mate  
  4.  
  5.     left join cat.kittens as kitten  

另外,一个“fetch”连接允许使用单连接来关联或值的集合,使它们可以和父对象一起来初始化。这在使用Collection的情况下特别有用。

  1. from eg.Cat as cat  
  2.  
  3.     inner join fetch cat.mate  
  4.  
  5.     left join fetch cat.kittens  

fetch join通常不需要设置别名,因为被关联的对象不应该被用在where从句中,也不能用在其他的任何从句中。

被关联的对象不能直接在查询结果中返回,他们可以通过父对象来访问。

请注意:在目前的实现中,在查询中只能返回一个集合。另外还要注意,fetch可能不用在被scroll()和iterator()调用的查询中。***还要注意,full join fetch和right join fetch是没有意义的。

4 select从句(The select clause)

select从句用来挑选在结果集中返回的对象和属性:

  1. select cat.mate from eg.Cat cat 

上面这个查询返回所有猫的配偶。

你也可以使用elements函数返回集合的元素。下面的查询将返回任何猫(Cat)的所有小猫(Kitten)。

  1. select elements(cat.kittens) from eg.Cat cat 

查询也可以返回任何值类型(包括Component类型的属性)的属性:

  1. select cat.name from eg.DomesticCat cat  
  2.  
  3. where cat.name like 'fri%' 
  4.  
  5. select cust.name.firstName from Customer as cust  

查询可以返回多个对象,也可以返回作为Object[]类型的数组的属性。

  1. select mother, offspr, mate.name 
  2.  
  3. from eg.DomesticCat as mother  
  4.  
  5.     inner join mother.mate as mate  
  6.  
  7.     left outer join mother.kittens as offspr  

或者作为一个实际的Java对象:

  1. select new Family(mother, mate, offspr)  
  2.  
  3. from eg.DomesticCat as mother  
  4.  
  5.     join mother.mate as mate  
  6.  
  7.     left join mother.kittens as offspr  

上面的这个查询语句假设Family类有适当的构造函数。

5 聚集函数(Aggregate functions)

查询可以使用属性的聚集函数:

  1. select avg(cat.weight), sum(cat.weight), max(cat.weight), count(cat)  from eg.Cat cat 

select从句的聚集函数中可以出现集合:

select cat, count( elements(cat.kittens) )  from eg.Cat cat group by cat

支持的聚集函数有:

· avg(...), sum(...), min(...), max(...)

· count(*)

· count(...), count(distinct ...), count(all...)

distinct 和all关键字的意义与用法和SQL中相同:

  1. select distinct cat.name from eg.Cat cat  
  2.  
  3. select count(distinct cat.name), count(cat) from eg.Cat cat  

6 多态(polymorphism)

一个查询:from eg.Cat as cat,它返回的不只是Cat,也有DomesticCat(家猫)这样的子类。Hibernate可以在from从句中指定任何Java类和接口,查询将返回继承自该类和实现了该接口的所有的持久类的实例。下面的查询将返回所有持久的对象:

  1. from java.lang.Object o 

指定的接口可以被多个不同的持久类实现:

  1. from eg.Named n, eg.Named m where n.name = m.name 

注意***2个查询将需要超过1个SQL的select,这意味着不能够按照从句指定的排列次序排列整个结果集。这也意味着你不能用Query.scroll()来调用这些查询。

7 where从句(The where clause)

where从句是你可以按照自己指定的条件更精确的返回实例:

  1. from eg.Cat as cat where cat.name='Fritz' 

复合表达式使where从句功能非常强大:

  1. from eg.Cat as cat where cat.name='Fritz'  

这个查询将被翻译成带有一个连接的SQL查询语句。

如果你写下这句查询:

  1. from eg.Foo foo where foo.bar.baz.customer.address.city is not null 

这个查询翻译成SQL语句需要4个表连接。

“=”操作符不仅可以比较属性,也可以比较实例:

  1. from eg.Cat cat, eg.Cat rival where cat.mate = rival.mate  
  2.  
  3. select cat, mate  from eg.Cat cat, eg.Cat mate  where cat.mate = mate  

一个叫id的特殊的属性被用来引用一个对象的唯一标识符,你也可以用对象的属性名。

  1. from eg.Cat as cat where cat.id = 123  
  2.  
  3. from eg.Cat as cat where cat.mate.id = 69    

这个查询要比上一个有效率,因为不需要表连接。

可以使用复合主键的属性。假设person有一个由medicareNumber和country构成的符合主键:

  1. from bank.Person person  
  2.  
  3. where person.id.country = 'AU' 
  4.  
  5. and person.id.medicareNumber = 123456  
  6.  
  7. from bank.Account account  
  8.  
  9. where account.owner.id.country = 'AU' 
  10.  
  11.     and account.owner.id.medicareNumber = 123456  

再重复一次,第二个查询效率高些。

同样,一个指定了属性的类在多态持久(polymorphic persistence)的情况下访问实体的discriminator value。

一个被嵌入到where从句中的Java类名将被翻译成它的discriminator value。

from eg.Cat cat where cat.class = eg.DomesticCat

你也可以指定组件(component)的属性和用户自己定义的合成类型(及组件的组件等等)。

永远也不要使用一个以组件的属性结尾的路径表达式。举个例子,假设store.owner是一个带有一个叫address组件的实体:

  1. store.owner.address.city    //正确  
  2.  
  3. store.owner.address        //错误!  

一个叫“any”的类型有2个特别的属性,一个是id,另一个是class,它允许我们用下面的办法进行连接(join)。AuditLog.item是一个用<  any>映射的属性:

  1. from eg.AuditLog log, eg.Payment payment  
  2.  
  3. where log.item.class = 'eg.Payment' and log.item.id = payment.id  

需要注意的是:查询中的log.item.class和payment.class将参考完全不同的数据库列。

8 表达式(Expressions)

where从句中的表达式允许你使用SQL中的很多东西:

· 数学运算符: +, -, *, /

· 二元比较运算符: =, >=, <  =, <  >, !=, like

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

· 字符串连接符: ||

· SQL函数,如: upper() and lower()

· 圆括号: ( )

· in, between, is null

· JDBC输入参数: ?

· 指定的参数::name, :start_date, :x1

· in和between:

  1. from eg.DomesticCat cat where cat.name between 'A' and 'B' 
  2.  
  3. from eg.DomesticCat cat where cat.name in ( 'Foo''Bar''Baz' )  

和否定形式的(negated forms):

  1. from eg.DomesticCat cat where cat.name not between 'A' and 'B' 
  2.  
  3. from eg.DomesticCat cat where cat.name not in ( 'Foo''Bar''Baz' )  

· is null和is not null

· 也可以使用特殊的属性size或size()函数来测试集合的大小:

  1. from eg.Cat cat where cat.kittens.size > 0  
  2.  
  3. from eg.Cat cat where size(cat.kittens) > 0  

·  对于有索引的集合,你可以使用特殊属性minIndex和maxIndex来引用最小索引和***索引。同样,你也可以使用minElement和maxElement来引用基本类型的集合的minimum和maximum元素。

  1. from Calendar cal where cal.holidays.maxElement > current date 

也可以是函数的形式:

  1. from Order order where maxindex(order.items) > 100  
  2.  
  3. from Order order where minelement(order.items) > 10000  

在传递索引和元素给集合时(elements and indices函数)和传递子查询的结果集时,SQL函数any, some, all, exists, in都是被支持的:

  1. select mother from eg.Cat as mother, eg.Cat as kit  
  2.  
  3. where kit in elements(foo.kittens)  
  4.  
  5. select p from eg.NameList list, eg.Person p  
  6.  
  7. where p.name = some elements(list.names)  
  8.  
  9. from eg.Cat cat where exists elements(cat.kittens)  
  10.  
  11. from eg.Player p where 3 > all elements(p.scores)  
  12.  
  13. from eg.Show show where 'fizard' in indices(show.acts)  

请注意:size, elements, indices, minIndex, maxIndex, minElement, maxElement在使用时有一些限制:

v      where从句中的in只用于数据库的子查询。

v      select从句中的in只用于elements 和indices函数。

v      带有索引的元素的collection(arrays, lists, maps)只能在where从句中通过索引引用:

  1. from Order order where order.items[0].id = 1234  
  2.  
  3. select person from Person person, Calendar calendar  
  4.  
  5. where calendar.holidays['national day'] = person.birthDay  
  6.  
  7.     and person.nationality.calendar = calendar  
  8.  
  9. select item from Item item, Order order 
  10.  
  11. where order.items[ order.deliveredItemIndices[0] ] = item and order.id = 11  
  12.  
  13. select item from Item item, Order order 
  14.  
  15. where order.items[ maxindex(order.items) ] = item and order.id = 11  

表达式中的[]的内部可以是一个算术表达式:

  1. select item from Item item, Order order 
  2.  
  3. where order.items[ size(order.items) - 1 ] = item  

HQL为one-to-many关联和值的集合提供了内置的index()函数:

  1. select item, index(item) from Order order 
  2.  
  3.     join order.items item  
  4.  
  5. where index(item) <  5  

被一些特定数据库支持的SQL函数可以被使用:

  1. from eg.DomesticCat cat where upper(cat.namelike 'FRI%' 

如果你还不相信上面的一切,那么想想更长的和更短的可读的查询吧:

  1. select cust  
  2. from Product prod,  
  3.     Store store  
  4.     inner join store.customers cust  
  5. where prod.name = 'widget' 
  6.     and store.location.name in ( 'Melbourne''Sydney' )  
  7.     and prod = all elements(cust.currentOrder.lineItems) 

提示:something like

  1. SELECT cust.name, cust.address, cust.phone, cust.id, cust.current_order  
  2. FROM customers cust,  
  3.     stores store,  
  4.     locations loc,  
  5.     store_customers sc,  
  6.     product prod  
  7. WHERE prod.name = 'widget' 
  8.     AND store.loc_id = loc.id  
  9.     AND loc.name IN ( 'Melbourne''Sydney' )  
  10.     AND sc.store_id = store.id  
  11.     AND sc.cust_id = cust.id  
  12.     AND prod.id = ALL(  
  13.         SELECT item.prod_id  
  14.         FROM line_items item, orders o  
  15.         WHERE item.order_id = o.id  
  16. AND cust.current_order = o.id) 

【编辑推荐】

  1. 使用MyEclipse开发***个Hibernate程序
  2. Hibernate 中Clob字段的使用
  3. Hibernate自动生成标
  4. Hibernate的三个状态(3)
  5. Hibernate的三个状态(2)
责任编辑:book05 来源: 百度博客
相关推荐

2009-06-18 09:14:08

Hibernate H

2016-03-10 10:07:22

设计首页开发

2009-09-23 18:05:48

2011-05-26 16:04:17

java

2009-09-25 16:57:49

Hibernate查询

2011-07-11 17:56:04

java

2016-10-28 16:53:03

数据库

2009-01-20 10:27:00

2009-06-12 15:32:01

Hibernate H

2016-03-17 10:29:03

NoSQL数据整合系统集成

2009-10-27 13:34:56

Oracle密码管理

2024-02-27 07:12:12

编程语言TS

2017-11-10 06:31:40

液冷管路连接机房

2016-03-22 10:35:05

移动·开发技术周刊

2016-12-02 16:25:09

数据中心模块化

2017-08-15 15:18:43

混合云云服务商管理

2015-01-29 09:11:37

OpenStack云应用云部署

2022-01-05 09:26:56

IT灾难IT故障

2019-05-27 23:21:47

大数据云迁移企业

2009-06-29 18:26:46

HibernateHQL查询
点赞
收藏

51CTO技术栈公众号