简述Hibernate部分查询语句

开发 后端
Hibernate配备了一种非常强大的Hibernate查询语句,这种语言看上去很像SQL。但是不要被语法结构上的相似所迷惑,HQL是非常有意识的被设计为完全面向对象的查询,它可理解如继承、多态和关联之类的概念。

Hibernate配备了一种非常强大的Hibernate查询语句,这种语言看上去很像SQL。本文主要介绍select子句、聚集函数、多态查询。但是不要被语法结构 上的相似所迷惑,HQL是非常有意识的被设计为完全面向对象的查询,它可以理解如继承、多态 和关联之类的概念。

1. select子句

select 子句选择将哪些对象与属性返 回到查询结果集中. 考虑如下情况:

select mate   
from Cat as cat   
    inner join cat.mate as mate 
  • 1.
  • 2.
  • 3.

该语句将选择mates of other Cats。(其他猫的配偶) 实际上, 你可以更简洁的用以下的查询语句表达相同的含义:

select cat.mate from Cat cat 
  • 1.

Hibernate查询语句可以返回值为任何类型的属性,包括返回类型为某种组件(Component)的属性:

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

Hibernate查询语句可以返回多个对象和(或)属性,存放在 Object[]队列中,

select mother, offspr, mate.name   
from DomesticCat as mother  
    inner join mother.mate as mate  
    left outer join mother.kittens as offspr 
  • 1.
  • 2.
  • 3.
  • 4.

或存放在一个List对象中,

select new list(mother, offspr, mate.name)  
from DomesticCat as mother  
    inner join mother.mate as mate  
    left outer join mother.kittens as offspr 
  • 1.
  • 2.
  • 3.
  • 4.

也可能直接返回一个实际的类型安全的Java对象,

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

假设类Family有一个合适的构造函数.

你可以使用关键字as给“被选择了的表达式”指派别名:

select max(bodyWeight) as max, min(bodyWeight) as min, count(*) as n  
from Cat cat 
  • 1.
  • 2.

这种做法在与子句select new map一起使用时最有用:

select new map( max(bodyWeight) as max, min(bodyWeight) as min, count(*) as n )  
from Cat cat 
  • 1.
  • 2.

该Hibernate查询语句返回了一个Map的对象,内容是别名与被选择的值组成的名-值映射。

3. 聚集函数

HQL查询甚至可以返回作用于属性之上的聚集函数的计算结果:

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

受支持的聚集函数如下:

avg(...), sum(...), min(...), max(...)   
 
count(*)   
 
count(...), count(distinct ...), count(all...)   
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

你可以在选择子句中使用数学操作符、连接以及经过验证的SQL函数:

select cat.weight + sum(kitten.weight)   
from Cat cat   
    join cat.kittens kitten  
group by cat.id, cat.weight  
select firstName||' '||initial||' '||upper(lastName) from Person  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

关键字distinct与all 也可以使用,它们具有与SQL相同的语义.

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

3. 多态查询

一个如下的Hibernate查询语句:

from Cat as cat 
  • 1.

不仅返回Cat类的实例, 也同时返回子类 DomesticCat的实例. Hibernate 可以在from子句中指定任何 Java 类或接口. 查询会返回继承了该类的所有持久化子类 的实例或返回声明了该接口的所有持久化类的实例。下面的查询语句返回所有的被持久化的对象:

from java.lang.Object o 
  • 1.

接口Named 可能被各种各样的持久化类声明:

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

注意:***的两个查询将需要超过一个的SQL SELECT.这表明order by子句 没有对整个结果集进行正确的排序. (这也说明你不能对这样的查询使用Query.scroll()方法.)

【编辑推荐】

  1. 简述Hibernate部分查询语言(一)
  2. Hibernate属性简单分析
  3. Struts-Spring-Hibernate案例
  4. Hibernate Sessin接口常用方法
  5. Hibernate事务全面介绍
责任编辑:仲衡 来源: redsaga
相关推荐

2009-09-24 10:15:37

Hibernate查询

2009-09-23 09:16:25

Hibernate复合

2009-09-25 16:57:49

Hibernate查询

2009-09-24 11:17:32

Hibernate查询

2009-09-29 16:41:58

Hibernate S

2011-03-24 11:37:41

Hibernate

2009-09-29 15:58:22

Hibernate映射

2009-09-29 10:37:29

Hibernate持久

2009-09-28 11:12:52

Hibernate O

2009-09-29 17:22:30

Hibernate S

2009-09-28 13:45:59

Hibernate历史

2009-09-25 12:31:13

Hibernate映射

2009-06-17 14:41:57

Hibernate查询

2009-09-22 16:04:50

Hibernate连接

2009-09-24 17:28:29

Hibernate S

2009-09-29 09:44:52

Hibernate事务

2009-09-24 18:11:56

Hibernate q

2009-11-04 08:53:57

VB.NET AddH

2009-09-25 14:04:09

Hibernate eHibernate h

2010-04-21 10:22:25

Unix 命令
点赞
收藏

51CTO技术栈公众号