Hibernate配備了一種非常強大的查詢語言,這種語言看上去很像SQL。但是不要被文法結構上的相似所迷惑,HQL是非常有意識的被設計為完全物件導向的查詢,它可以理解如繼承、多態 和關聯之類的概念。
1.大小寫敏感性問題
除了Java類與屬性的名稱外,查詢語句對大小寫並不敏感。 所以 SeLeCT 與 sELEct 以及 SELECT 是相同的,但是 org.hibernate.eg.FOO 並不等價於 org.hibernate.eg.Foo 並且 foo.barSet 也不等價於 foo.BARSET。
本手冊中的HQL關鍵字將使用小寫字母. 很多使用者發現使用完全大寫的關鍵字會使查詢語句 的可讀性更強, 但我們發現,當把查詢語句嵌入到Java語句中的時候使用大寫關鍵字比較難看。
2.from子句
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)。
3.關聯(Association)與串連(Join)
我們也可以為相關聯的實體甚至是對一個集合中的全部元素指定一個別名, 這時要使用關鍵字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 join(內串連)
left outer join(左外串連)
right outer join(右外串連)
full join (全串連,並不常用)
語句inner join, left outer join 以及 right outer join 可以簡寫。
from Cat as cat join cat.mate as mate left join cat.kittens as kitten
還有,一個"fetch"串連允許僅僅使用一個選擇語句就將相關聯的對象或一組值的集合隨著他們的父物件的初始化而被初始化,這種方法在使用到集合的情況下尤其有用,對於關聯和集合來說,它有效代替了對應檔中的外聯結與延遲聲明(lazy declarations). 以獲得等多的資訊。
from Cat as cat inner join fetch cat.mate left join fetch cat.kittens
一個fetch串連通常不需要被指定別名, 因為相關聯的對象不應當被用在 where 子句 (或其它任何子句)中。同時,相關聯的對象並不在查詢的結果中直接返回,但可以通過他們的父物件來訪問到他們。
注意fetch構造變數在使用了scroll() 或 iterate()函數的查詢中是不能使用的。最後注意,使用full join fetch 與 right 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%'
4.select子句
select 子句選擇將哪些對象與屬性返回到查詢結果集中. 考慮如下情況:
select mate from Cat as cat inner join cat.mate as mate
該語句將選擇mates of other Cats。(其他貓的配偶) 實際上, 你可以更簡潔的用以下的查詢語句表達相同的含義:
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
也可能直接返回一個實際的型別安全的Java對象,
select new Family(mother, mate, offspr) from DomesticCat as mother join mother.mate as mate left join mother.kittens as offspr
假設類Family有一個合適的建構函式.
你可以使用關鍵字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的對象,內容是別名與被選擇的值組成的名-值對應。
5.聚集合函式
HQL查詢甚至可以返回作用於屬性之上的聚集合函式的計算結果:
select avg(cat.weight), sum(cat.weight), max(cat.weight), count(cat) from Cat cat
受支援的聚集合函式如下:
avg(...), sum(...), min(...), max(...)
count(*)
count(...), count(distinct ...), count(all...)
你可以在選擇子句中使用數學操作符、串連以及經過驗證的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
關鍵字distinct與all 也可以使用,它們具有與SQL相同的語義.
select distinct cat.name from Cat cat select count(distinct cat.name), count(cat) from Cat cat
6.多態查詢
一個如下的查詢語句:
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()方法.)
7.where子句
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屬性等於 Foo的startDate屬性。 複合路徑運算式使得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
特殊屬性(小寫)id可以用來表示一個對象的唯一的標識符。(你也可以使用該對象的屬性名稱。)
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
第二個查詢也不需要進行表串連。
同樣的,特殊屬性class在進行多態持久化的情況下被用來存取一個執行個體的鑒別值(discriminator value)。 一個嵌入到where子句中的Java類的名字將被轉換為該類的鑒別值。
from Cat cat where cat.class = DomesticCat
你也可以聲明一個屬性的類型是組件或者複合使用者類型(以及由組件構成的組件等等)。永遠不要嘗試使用以組件類型來結尾的路徑運算式(path-expression_r)(與此相反,你應當使用組件的一個屬性來結尾)。 舉例來說,如果store.owner含有一個包含了組件的實體address
store.owner.address.city // 正確 store.owner.address // 錯誤!
一個“任意”類型有兩個特殊的屬性id和class, 來允許我們按照下面的方式表達一個串連(AuditLog.item 是一個屬性,該屬性被映射為<any>)。
from AuditLog log, Payment payment where log.item.class = 'Payment' and log.item.id = payment.id
注意,在上面的查詢與句中,log.item.class 和 payment.class 將涉及到完全不同的資料庫中的列。
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, :x1
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函數來引用到一個基礎資料型別 (Elementary Data Type)的集合中最小與最大的元素。
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 )
9.order by子句
查詢返回的列表(list)可以按照一個返回的類或組件(components)中的任何屬性(property)進行排序:
from DomesticCat cat order by cat.name asc, cat.weight desc, cat.birthdate
可選的asc或desc關鍵字指明了按照升序或降序進行排序.
10.group by子句
一個返回聚集值(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的一般函數與聚集合函式也可以出現 在having與order by 子句中。
select cat from 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子句與 order by子句中都不能包含算術運算式(arithmetic expression_rs).