Hibernate.orgCommunity Documentation

第 16 章 HQL: Hibernate 查询语言

16.1. 大小写敏感性问题
16.2. from 子句
16.3. 关联(Association)与连接(Join)
16.4. join 语法的形式
16.5. 引用 identifier 属性
16.6. select 子句
16.7. 聚集函数
16.8. 多态查询
16.9. where 子句
16.10. 表达式
16.11. order by 子句
16.12. group by 子句
16.13. 子查询
16.14. HQL 示例
16.15. 批量的 UPDATE 和 DELETE
16.16. 小技巧 & 小窍门
16.17. 组件
16.18. Row value 构造函数语法

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

除了 Java 类与属性的名称外,查询语句对大小写并不敏感。 所以 SeLeCTsELEct 以及 SELECT 是相同的,但是 org.hibernate.eg.FOO 并不等价于 org.hibernate.eg.Foo 并且 foo.barSet 也不等价于 foo.BARSET

本手册中的 HQL 关键字将使用小写字母。很多用户发现使用完全大写的关键字会使查询语句的可读性更强,但我们发现,当把查询语句嵌入到 Java 语句中的时候使用大写关键字比较难看。

Hibernate 中最简单的查询语句的形式如下:

from eg.Cat

该子句简单的返回 eg.Cat 类的所有实例。通常我们不需要使用类的全限定名,因为 auto-import(自动引入)是缺省的情况。所以我们几乎只使用如下的简单写法:

from Cat

为了在这个查询的其他部分里引用 Cat,你将需要分配一个别名。例如:

from Cat as cat

这个语句把别名 cat 指定给类Cat 的实例,这样我们就可以在随后的查询中使用此别名了。关键字 as 是可选的,我们也可以这样写:

from Cat cat

子句中可以同时出现多个类,其查询结果是产生一个笛卡儿积或产生跨表的连接。

from Formula, Parameter
from Formula as form, Parameter as param

查询语句中别名的开头部分小写被认为是实践中的好习惯,这样做与 Java 变量的命名标准保持了一致(比如,domesticCat)。

我们也可以为相关联的实体甚至是对一个集合中的全部元素指定一个别名,这时要使用关键字 join

from Cat as cat
    inner join cat.mate as mate
    left outer join cat.kittens as kitten
from Cat as cat left join cat.mate.kittens as kittens
from Formula form full join form.parameter param

受支持的连接类型是从 ANSI SQL 中借鉴来的:

语句 inner joinleft outer join 以及 right outer join 可以简写。

from Cat as cat
    join cat.mate as mate
    left join cat.kittens as kitten

通过 HQL 的 with 关键字,你可以提供额外的 join 条件。

from Cat as cat
    left join cat.kittens as kitten
        with kitten.bodyWeight 
> 10.0

A "fetch" join allows associations or collections of values to be initialized along with their parent objects using a single select. This is particularly useful in the case of a collection. It effectively overrides the outer join and lazy declarations of the mapping file for associations and collections. See 第 21.1 节 “抓取策略(Fetching strategies)” for more information.

from Cat as cat
    inner join fetch cat.mate
    left join fetch cat.kittens

一个 fetch 连接通常不需要被指定别名,因为相关联的对象不应当被用在 where 子句(或其它任何子句)中。同时,相关联的对象并不在查询的结果中直接返回,但可以通过他们的父对象来访问到他们。

from Cat as cat
    inner join fetch cat.mate
    left join fetch cat.kittens child
    left join fetch child.kittens

假若使用 iterate() 来调用查询,请注意 fetch 构造是不能使用的(scroll() 可以使用)。fetch 也不应该与 setMaxResults()setFirstResult() 共用,这是因为这些操作是基于结果集的,而在预先抓取集合类时可能包含重复的数据,也就是说无法预先知道精确的行数。fetch 还不能与独立的 with 条件一起使用。通过在一次查询中 fetch 多个集合,可以制造出笛卡尔积,因此请多加注意。对 bag 映射来说,同时 join fetch 多个集合角色可能在某些情况下给出并非预期的结果,也请小心。最后注意,使用 full join fetchright join fetch 是没有意义的。

如果你使用属性级别的延迟获取(lazy fetching)(这是通过重新编写字节码实现的),可以使用 fetch all properties 来强制 Hibernate 立即取得那些原本需要延迟加载的属性(在第一个查询中)。

from Document fetch all properties order by name
from Document doc fetch all properties where lower(doc.name) like '%cats%'

HQL 支持两种关联 join 的形式:implicit(隐式)explicit(显式)

上一节中给出的查询都是使用 explicit(显式)形式的,其中 form 子句中明确给出了 join 关键字。这是建议使用的方式。

implicit(隐式)形式不使用 join 关键字。关联使用"点号"来进行“引用”。implicit join 可以在任何 HQL 子句中出现。implicit join 在最终的 SQL 语句中以 inner join 的方式出现。

from Cat as cat where cat.mate.name like '%s%'

通常有两种方法来引用实体的 identifier 属性:

对组合 identifier 属性的引用遵循相同的命名规则。如果实体有一个 non-identifier 属性命名的 id,这个组合 identifier 属性只能用自己定义的名字来引用;否则,特殊 id 属性可以用来引用 identifier 属性。

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

select mate
from Cat as cat
    inner join cat.mate as mate

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

select cat.mate from Cat cat

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

select cat.name from DomesticCat cat
where cat.name like 'fri%'
select cust.name.firstName from Customer as cust

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

select mother, offspr, mate.name
from DomesticCat as mother
    inner join mother.mate as mate
    left outer join mother.kittens as offspr

或存放在一个 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

假设类 Family 有一个合适的构造函数 - 作为实际的类型安全的 Java 对象:

select new Family(mother, mate, offspr)
from DomesticCat as mother
    join mother.mate as mate
    left join mother.kittens as offspr

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

select max(bodyWeight) as max, min(bodyWeight) as min, count(*) as n
from Cat cat

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

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

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

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

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

受支持的聚集函数如下:

你可以在选择子句中使用数学操作符、连接以及经过验证的 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

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

select distinct cat.name from Cat cat

select count(distinct cat.name), count(cat) from Cat cat

一个如下的查询语句:

from Cat as cat

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

from java.lang.Object o

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

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

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

where 子句允许你将返回的实例列表的范围缩小。如果没有指定别名,你可以使用属性名来直接引用属性:

from Cat where name='Fritz'

如果指派了别名,需要使用完整的属性名:

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

返回名为(属性 name 等于)'Fritz' 的 Cat 类的实例。

下面的查询:

select foo
from Foo foo, Bar bar
where foo.startDate = bar.date

将返回所有满足下面条件的 Foo 类的实例: 存在如下的 bar 的一个实例,其 date 属性等于 FoostartDate 属性。复合路径表达式使得 where 子句非常的强大,考虑如下情况:

from Cat cat where cat.mate.name is not null

该查询将被翻译成为一个含有表连接(内连接)的 SQL 查询。如果你打算写像这样的查询语句:

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

在 SQL 中,你为达此目的将需要进行一个四表连接的查询。

= 运算符不仅可以被用来比较属性的值,也可以用来比较实例:

from Cat cat, Cat rival where cat.mate = rival.mate
select cat, mate
from Cat cat, Cat mate
where cat.mate = mate

The special property (lowercase) id can be used to reference the unique identifier of an object. See 第 16.5 节 “引用 identifier 属性 ” for more information.

from Cat as cat where cat.id = 123

from Cat as cat where cat.mate.id = 69

第二个查询是有效的。此时不需要进行表连接。

同样也可以使用复合标识符。比如 Person 类有一个复合标识符,它由 country 属性与 medicareNumber 属性组成:

from bank.Person person
where person.id.country = 'AU'
    and person.id.medicareNumber = 123456
from bank.Account account
where account.owner.id.country = 'AU'
    and account.owner.id.medicareNumber = 123456

第二个查询也不需要进行表连接。

See 第 16.5 节 “引用 identifier 属性 ” for more information regarding referencing identifier properties)

同样的,特殊属性 class 在进行多态持久化的情况下被用来存取一个实例的鉴别值(discriminator value)。一个嵌入到 where 子句中的 Java 类的名字将被转换为该类的鉴别值。

from Cat cat where cat.class = DomesticCat

You can also use components or composite user types, or properties of said component types. See 第 16.17 节 “组件” for more information.

一个“任意”类型有两个特殊的属性 idclass,来允许我们按照下面的方式表达一个连接(AuditLog.item 是一个属性,该属性被映射为 <any>)。

from AuditLog log, Payment payment
where log.item.class = 'Payment' and log.item.id = payment.id

注意,在上面的查询与句中,log.item.classpayment.class 将涉及到完全不同的数据库中的列。

where 子句中允许使用的表达式包括 大多数你可以在 SQL 使用的表达式种类:

关键字 inbetween 可按如下方法使用:

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 nullis not null 可以被用来测试空值(null)。

在 Hibernate 配置文件中声明 HQL“查询替代(query substitutions)”之后,布尔表达式(Booleans)可以在其他表达式中轻松的使用:

<property name="hibernate.query.substitutions"
>true 1, false 0</property
>

系统将该 HQL 转换为 SQL 语句时,该设置表明将用字符 10 来取代关键字 truefalse

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

对于索引了(有序)的集合,你可以使用 minindexmaxindex 函数来引用到最小与最大的索引序数。同理,你可以使用 minelementmaxelement 函数来引用到一个基本数据类型的集合中最小与最大的元素。例如:

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

在传递一个集合的索引集或者是元素集(elementsindices 函数)或者传递一个子查询的结果的时候,可以使用 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 中,这些结构变量 — sizeelementsindicesminindexmaxindexminelementmaxelement — 只能在 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
    )

查询返回的列表(list)可以按照一个返回的类或组件(components)中的任何属性(property)进行排序:

from DomesticCat cat
order by cat.name asc, cat.weight desc, cat.birthdate

可选的 ascdesc 关键字指明了按照升序或降序进行排序。

一个返回聚集值(aggregate values)的查询可以按照一个返回的类或组件(components)中的任何属性(property)进行分组:

select cat.color, sum(cat.weight), count(cat)
from Cat cat
group by cat.color
select foo.id, avg(name), max(name)
from Foo foo join foo.names name
group by foo.id

having 子句在这里也允许使用。

select cat.color, sum(cat.weight), count(cat)
from Cat cat
group by cat.color
having cat.color in (eg.Color.TABBY, eg.Color.BLACK)

如果底层的数据库支持的话(例如不能在 MySQL 中使用),SQL 的一般函数与聚集函数也可以出现在 havingorder by 子句中。

select cat
from Cat cat
    join cat.kittens kitten
group by cat.id, cat.name, cat.other, cat.properties
having avg(kitten.weight) 
> 100
order by count(kitten) asc, sum(kitten.weight) desc

注意 group by 子句与 order by 子句中都不能包含算术表达式(arithmetic expressions)。也要注意 Hibernate 目前不会扩展 group 的实体,因此你不能写 group by cat,除非 cat 的所有属性都不是聚集的(non-aggregated)。你必须明确的列出所有的非聚集属性。

对于支持子查询的数据库,Hibernate 支持在查询中使用子查询。一个子查询必须被圆括号包围起来(经常是 SQL 聚集函数的圆括号)。甚至相互关联的子查询(引用到外部查询中的别名的子查询)也是允许的。

from Cat as fatcat
where fatcat.weight 
> (
    select avg(cat.weight) from DomesticCat cat
)
from DomesticCat as cat
where cat.name = some (
    select name.nickName from Name as name
)
from Cat as cat
where not exists (
    from Cat as mate where mate.mate = cat
)
from DomesticCat as cat
where cat.name not in (
    select name.nickName from Name as name
)
select cat.id, (select max(kit.weight) from cat.kitten kit)
from Cat as cat

注意,HQL 自查询只可以在 select 或者 where 子句中出现。

Note that subqueries can also utilize row value constructor syntax. See 第 16.18 节 “Row value 构造函数语法” for more information.

Hibernate 查询可以非常的强大与复杂。实际上,Hibernate 的一个主要卖点就是查询语句的威力。这里有一些例子,它们与我在最近的一个项目中使用的查询非常相似。注意你能用到的大多数查询比这些要简单的多。

下面的查询对于某个特定的客户的所有未支付的账单,在给定给最小总价值的情况下,返回订单的 id,条目的数量和总价值,返回值按照总价值的结果进行排序。为了决定价格,查询使用了当前目录。作为转换结果的 SQL 查询,使用了ORDERORDER_LINEPRODUCTCATALOGPRICE 库表。

select order.id, sum(price.amount), count(item)
from Order as order
    join order.lineItems as item
    join item.product as product,
    Catalog as catalog
    join catalog.prices as price
where order.paid = false
    and order.customer = :customer
    and price.product = product
    and catalog.effectiveDate < sysdate
    and catalog.effectiveDate 
>= all (
        select cat.effectiveDate
        from Catalog as cat
        where cat.effectiveDate < sysdate
    )
group by order
having sum(price.amount) 
> :minAmount
order by sum(price.amount) desc

这简直是一个怪物!实际上,在现实生活中,我并不热衷于子查询,所以我的查询语句看起来更像这个:

select order.id, sum(price.amount), count(item)
from Order as order
    join order.lineItems as item
    join item.product as product,
    Catalog as catalog
    join catalog.prices as price
where order.paid = false
    and order.customer = :customer
    and price.product = product
    and catalog = :currentCatalog
group by order
having sum(price.amount) 
> :minAmount
order by sum(price.amount) desc

下面一个查询计算每一种状态下的支付的数目,除去所有处于 AWAITING_APPROVAL 状态的支付,因为在该状态下 当前的用户作出了状态的最新改变。该查询被转换成含有两个内连接以及一个相关联的子选择的 SQL 查询,该查询使用了表 PAYMENTPAYMENT_STATUS 以及 PAYMENT_STATUS_CHANGE

select count(payment), status.name
from Payment as payment
    join payment.currentStatus as status
    join payment.statusChanges as statusChange
where payment.status.name <
> PaymentStatus.AWAITING_APPROVAL
    or (
        statusChange.timeStamp = (
            select max(change.timeStamp)
            from PaymentStatusChange change
            where change.payment = payment
        )
        and statusChange.user <
> :currentUser
    )
group by status.name, status.sortOrder
order by status.sortOrder

如果我把 statusChanges 实例集映射为一个列表(list)而不是一个集合(set),书写查询语句将更加简单。

select count(payment), status.name
from Payment as payment
    join payment.currentStatus as status
where payment.status.name <
> PaymentStatus.AWAITING_APPROVAL
    or payment.statusChanges[ maxIndex(payment.statusChanges) ].user <
> :currentUser
group by status.name, status.sortOrder
order by status.sortOrder

下面一个查询使用了 MS SQL Server 的 isNull() 函数用以返回当前用户所属组织的组织帐号及组织未支付的账。它被转换成一个对表 ACCOUNTPAYMENTPAYMENT_STATUSACCOUNT_TYPEORGANIZATION 以及 ORG_USER 进行的三个内连接,一个外连接和一个子选择的 SQL 查询。

select account, payment
from Account as account
    left outer join account.payments as payment
where :currentUser in elements(account.holder.users)
    and PaymentStatus.UNPAID = isNull(payment.currentStatus.name, PaymentStatus.UNPAID)
order by account.type.sortOrder, account.accountNumber, payment.dueDate

对于一些数据库,我们需要弃用(相关的)子选择。

select account, payment
from Account as account
    join account.holder.users as user
    left outer join account.payments as payment
where :currentUser = user
    and PaymentStatus.UNPAID = isNull(payment.currentStatus.name, PaymentStatus.UNPAID)
order by account.type.sortOrder, account.accountNumber, payment.dueDate

HQL now supports update, delete and insert ... select ... statements. See 第 15.4 节 “DML(数据操作语言)风格的操作(DML-style operations)” for more information.

你可以统计查询结果的数目而不必实际的返回他们:

( (Integer) session.createQuery("select count(*) from ....").iterate().next() ).intValue()

若想根据一个集合的大小来进行排序,可以使用如下的语句:

select usr.id, usr.name
from User as usr
    left join usr.messages as msg
group by usr.id, usr.name
order by count(msg)

如果你的数据库支持子选择,你可以在你的查询的 where 子句中为选择的大小(selection size)指定一个条件:

from User usr where size(usr.messages) 
>= 1

如果你的数据库不支持子选择语句,使用下面的查询:

select usr.id, usr.name
from User usr
    join usr.messages msg
group by usr.id, usr.name
having count(msg) 
>= 1

因为内连接(inner join)的原因,这个解决方案不能返回含有零个信息的 User 类的实例,所以这种情况下使用下面的格式将是有帮助的:

select usr.id, usr.name
from User as usr
    left join usr.messages as msg
group by usr.id, usr.name
having count(msg) = 0

JavaBean 的属性可以被绑定到一个命名查询(named query)的参数上:

Query q = s.createQuery("from foo Foo as foo where foo.name=:name and foo.size=:size");

q.setProperties(fooBean); // fooBean has getName() and getSize()
List foos = q.list();

通过将接口 Query 与一个过滤器(filter)一起使用,集合(Collections)是可以分页的:

Query q = s.createFilter( collection, "" ); // the trivial filter

q.setMaxResults(PAGE_SIZE);
q.setFirstResult(PAGE_SIZE * pageNumber);
List page = q.list();

通过使用查询过滤器(query filter)可以将集合(Collection)的元素分组或排序:

Collection orderedCollection = s.filter( collection, "order by this.amount" );

Collection counts = s.filter( collection, "select this.type, count(this) group by this.type" );

不用通过初始化,你就可以知道一个集合(Collection)的大小:

( (Integer) session.createQuery("select count(*) from ....").iterate().next() ).intValue();

在 HQL 查询里,组件可以和简单值类型一样使用。它们可以出现在 select 子句里:

select p.name from Person p
select p.name.first from Person p

在这里,Person 的 name 属性是一个组件。组件也可以用在 where 子句里:

from Person p where p.name = :name
from Person p where p.name.first = :firstName

组件也可以用在 order by 子句里:

from Person p order by p.name
from Person p order by p.name.first

组件的另外一个常见用法是在 第 16.18 节 “Row value 构造函数语法” 行值(row value)构造函数里。

HQL 支持 ANSI SQL row value constructor 语法(有时也叫作 tuple 语法),即使底层数据库可能不支持这个概念。在这里我们通常指的是多值(multi-valued)的比较,典型地是和组件相关联。来看看一个定义了 name 组件的实体 Person:

from Person p where p.name.first='John' and p.name.last='Jingleheimer-Schmidt'

那是有效的语法,虽然有点冗长。我们可以使它更加简洁一点,并使用 row value constructor 语法:

from Person p where p.name=('John', 'Jingleheimer-Schmidt')

select 子句里指定这个也是很有用的:

select p.name from Person p

当使用需要比较多个值的子查询时,采用 row value constructor 语法也很有用处:

from Cat as cat
where not ( cat.name, cat.color ) in (
    select cat.name, cat.color from DomesticCat cat
)

决定是否使用这个语法的一件因素就是:这个查询将依赖于元数据里的组件子属性(sub-properties)的顺序。