兔八哥笔记14(2):Hibernate 查询语言(HQL)  
10.7 where从句(The where clause)
       where从句是你可以按照自己指定的条件更精确的返回实例:
from eg.Cat as cat where cat.name='Fritz' 
复合表达式使where从句功能非常强大:
from eg.Cat as cat where cat.name='Fritz'  
这个查询将被翻译成带有一个连接的SQL查询语句。
       如果你写下这句查询:
       from eg.Foo foo where foo.bar.baz.customer.address.city is not null 
这个查询翻译成SQL语句需要4个表连接。
       “=”操作符不仅可以比较属性,也可以比较实例:
from eg.Cat cat, eg.Cat rival where cat.mate = rival.mate
 
select cat, mate  from eg.Cat cat, eg.Cat mate  where cat.mate = mate
 
一个叫id的特殊的属性被用来引用一个对象的唯一标识符,你也可以用对象的属性名。
from eg.Cat as cat where cat.id = 123
 
from eg.Cat as cat where cat.mate.id = 69  这个查询要比上一个有效率,因为不需要表连接。
可以使用复合主键的属性。假设person有一个由medicareNumber和country构成的符合主键:
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
再重复一次,第二个查询效率高些。
 
同样,一个指定了属性的类在多态持久(polymorphic persistence)的情况下访问实体的discriminator value。
一个被嵌入到where从句中的Java类名将被翻译成它的discriminator value。
from eg.Cat cat where cat.class = eg.DomesticCat
你也可以指定组件(component)的属性和用户自己定义的合成类型(及组件的组件等等)。
永远也不要使用一个以组件的属性结尾的路径表达式。举个例子,假设store.owner是一个带有一个叫address组件的实体:
store.owner.address.city    //正确
store.owner.address        //错误!
一个叫“any”的类型有2个特别的属性,一个是id,另一个是class,它允许我们用下面的办法进行连接(join)。AuditLog.item是一个用<any>映射的属性:
from eg.AuditLog log, eg.Payment payment 
where log.item.class = 'eg.Payment' and log.item.id = payment.id
需要注意的是:查询中的log.item.class和payment.class将参考完全不同的数据库列。
10.8 表达式(Expressions)
       where从句中的表达式允许你使用SQL中的很多东西:
·                                 数学运算符: +, -, *, / 
·                                 二元比较运算符: =, >=, <=, <>, !=, like 
·                                 逻辑操作符: and, or, not 
·                                 字符串连接符: || 
·                                 SQL函数,如: upper() and lower() 
·                                 圆括号: ( )  
·                                 in, between, is null 
·                                 JDBC输入参数: ? 
·                                 指定的参数::name, :start_date, :x1
·                                 in和between:
from eg.DomesticCat cat where cat.name between 'A' and 'B'
 
from eg.DomesticCat cat where cat.name in ( 'Foo', 'Bar', 'Baz' )
和否定形式的(negated forms):
from eg.DomesticCat cat where cat.name not between 'A' and 'B'
 
from eg.DomesticCat cat where cat.name not in ( 'Foo', 'Bar', 'Baz' )
·                                 is null和is not null
·                                 也可以使用特殊的属性size或size()函数来测试集合的大小:
from eg.Cat cat where cat.kittens.size > 0
 
from eg.Cat cat where size(cat.kittens) > 0
·                                 对于有索引的集合,你可以使用特殊属性minIndex和maxIndex来引用最小索引和最大索引。同样,你也可以使用minElement和maxElement来引用基本类型的集合的minimum和maximum元素。
from Calendar cal where cal.holidays.maxElement > current date
也可以是函数的形式:
from Order order where maxindex(order.items) > 100
 
from Order order where minelement(order.items) > 10000
在传递索引和元素给集合时(elements and indices函数)和传递子查询的结果集时,SQL函数any, some, all, exists, in都是被支持的:
select mother from eg.Cat as mother, eg.Cat as kit
where kit in elements(foo.kittens)
 
select p from eg.NameList list, eg.Person p
where p.name = some elements(list.names)
 
from eg.Cat cat where exists elements(cat.kittens)
 
from eg.Player p where 3 > all elements(p.scores)
 
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从句中通过索引引用:
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
HQL为one-to-many关联和值的集合提供了内置的index()函数:
select item, index(item) from Order order 
    join order.items item
where index(item) < 5
被一些特定数据库支持的SQL函数可以被使用:
from eg.DomesticCat cat where upper(cat.name) like 'FRI%'
如果你还不相信上面的一切,那么想想更长的和更短的可读的查询吧:
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)
 
提示:something like
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)
 
 
10.9 order by从句(The order by clause)
查询返回的列表可以使用组件或类的任意属性排序:
from eg.DomesticCat cat 
order by cat.name asc, cat.weight desc, cat.birthdate
关键字asc和desc是可选的,分别决定是按升序还是降序排列结果。
10.10 group by从句(The group by clause)
       查询返回的聚集可以按照指定的类或组件的任意属性进行分组:
select cat.color, sum(cat.weight), count(cat) 
from eg.Cat cat
group by cat.color
 
select foo.id, avg( elements(foo.names) ), max( indices(foo.names) ) 
from eg.Foo foo
group by foo.id
注意:你可以在select从句中使用elements和indices结构,即使数据库中没有子查询。
    Having从句也允许使用:
select cat.color, sum(cat.weight), count(cat) 
from eg.Cat cat
group by cat.color 
having cat.color in (eg.Color.TABBY, eg.Color.BLACK)
如果你的数据库支持,则你可以在having和order by从句中使用SQL函数(ie:MySQL中不支持):
select cat  from eg.Cat cat  join cat.kittens kitten
group by cat having avg(kitten.weight) > 100
order by count(kitten) asc, sum(kitten.weight) desc
注意:group by clause nor the order by都不支持算术表达式。