閱讀228 返回首頁    go 阿裏雲 go 技術社區[雲棲]


第十一章 Hibernate的查詢 HQL麵向對象的查詢語言

HQL基本上與sql語句在語法上相仿,隻是HQL具有麵向對象的特性而已。(本文部分內容是從網絡中獲取而來)

1. 大小寫敏感性問題


         除了Java類與屬性的名稱外,查詢語句對大小寫並不敏感。所以SeLeCTsELEct以及SELECT 是相同的,但是org.hibernate.eg.FOO並不等價於org.hibernate.eg.Foo並且foo.barSet也不等價於foo.BARSET


        本文中的HQL關鍵字將使用小寫字母.很多用戶發現使用完全大寫的關鍵字會使查詢語句的可讀性更強,但我們發現,當把查詢語句嵌入到Java語句中的時候使用大寫關鍵字比較難看。


2 from子句

Hibernate中最簡單的查詢語句的形式如下:

from cn.framelife.hql.Cat


該子句簡單的返回cn.framelife.hql.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

受支持的連接類型是從ANSISQL中借鑒來的。


innerjoin(內連接)


leftouter join(左外連接)


rightouter join(右外連接)


fulljoin (全連接,並不常用)


語句innerjoin, left outer join以及rightouter join可以簡寫。

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



還有,一個"fetch"連接允許僅僅使用一個選擇語句就將相關聯的對象或一組值的集合隨著他們的父對象的初始化而被初始化,這種方法在使用到集合的情況下尤其有用,對於關聯和集合來說,它有效的代替了映射文件中的外聯接與延遲聲明(lazydeclarations.查看第20.1節 “抓取策略(Fetching strategies)”以獲得等多的信息。

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


一個fetch連接通常不需要被指定別名,因為相關聯的對象不應當被用在where子句(或其它任何子句)中。同時,相關聯的對象並不在查詢的結果中直接返回,但可以通過他們的父對象來訪問到他們。


注意fetch構造變量在使用了scroll()iterate()函數的查詢中是不能使用的。最後注意,使用fulljoin fetchright joinfetch是沒有意義的。


如果你使用屬性級別的延遲獲取(lazyfetching)(這是通過重新編寫字節碼實現的),可以使用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

該語句將選擇matesof other Cats。(其他貓的配偶) 實際上,你可以更簡潔的用以下的查詢語句表達相同的含義:

select cat.mate from Cat cat

查詢語句可以返回值為任何類型的屬性,包括返回類型為某種組件(Component)的屬性:

selec tcat.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(*) asn ) 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


關鍵字distinctall也可以使用,它們具有與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

注意,最後的兩個查詢將需要超過一個的SQLSELECT.這表明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 fromFoo 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

特殊屬性(小寫)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在進行多態持久化的情況下被用來存取一個實例的鑒別值(discriminatorvalue)。 一個嵌入到where子句中的Java類的名字將被轉換為該類的鑒別值。

from Cat cat where cat.class = DomesticCat

你也可以聲明一個屬性的類型是組件或者複合用戶類型(以及由組件構成的組件等等)。永遠不要嚐試使用以組件類型來結尾的路徑表達式(path-expression)(與此相反,你應當使用組件的一個屬性來結尾)。舉例來說,如果store.owner含有一個包含了組件的實體address

store.owner.address.city // 正確
store.owner.address // 錯誤!

一個“任意”類型有兩個特殊的屬性idclass,來允許我們按照下麵的方式表達一個連接(AuditLog.item是一個屬性,該屬性被映射為<any>)。

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

注意,在上麵的查詢與句中,log.item.classpayment.class將涉及到完全不同的數據庫中的列。


11.2.8. 表達式

where子句中允許使用的表達式包括大多數你可以在SQL使用的表達式種類:


數學運算符+,-, *, /


二進製比較運算符=,>=, <=, <>, !=, like


邏輯運算符and,or, not


in,not in, between, is null, is not null, is empty, is not empty, memberof 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-QL3.0定義的任何函數或操作:substring(),trim(), lower(), upper(), length(), locate(), abs(), sqrt(),bit_length()


coalesce()nullif()


cast(...as ...),其第二個參數是某Hibernate類型的名字,以及extract(...from ...),隻要ANSIcast()extract()被底層數據庫支持


任何數據庫支持的SQL標量函數,比如sign(),trunc(), rtrim(), sin()


JDBC參數傳入?


命名參數:name,:start_date, :x1


SQL直接常量'foo', 69,'1970-01-01 10:00:01.0'


Javapublic static final類型的常量eg.Color.TABBY


關鍵字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 nullisnot null可以被用來測試空值(null).


Hibernate配置文件中聲明HQL“查詢替代(querysubstitutions)”之後, 布爾表達式(Booleans)可以在其他表達式中輕鬆的使用:

<propertyname="hibernate.query.substitutions">true 1, false0</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)
selec tp from NameList list, Person p wherep.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-manyassociation)或是值的集合中的元素,HQL也提供內建的index()函數,

select item, index(item) from Order order join order.items item whereindex(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
WHEREprod.name = 'widget'
ANDstore.loc_id = loc.id
ANDloc.name IN ( 'Melbourne', 'Sydney' )
ANDsc.store_id = store.id
ANDsc.cust_id = cust.id
ANDprod.id = ALL(
SELECTitem.prod_id
FROMline_items item, orders o
WHEREitem.order_id = o.id
ANDcust.current_order = o.id
)



11.2.9. orderby子句


查詢返回的列表(list)可以按照一個返回的類或組件(components)中的任何屬性(property)進行排序:

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

可選的ascdesc關鍵字指明了按照升序或降序進行排序.


11.2.10. groupby子句

一個返回聚集值(aggregatevalues)的查詢可以按照一個返回的類或組件(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的一般函數與聚集函數也可以出現在havingorderby子句中。

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

注意groupby子句與 orderby子句中都不能包含算術表達式(arithmeticexpressions)

.


11.2.11. 子查詢

對於支持子查詢的數據庫,Hibernate支持在查詢中使用子查詢。一個子查詢必須被圓括號包圍起來(經常是SQL聚集函數的圓括號)。甚至相互關聯的子查詢(引用到外部查詢中的別名的子查詢)也是允許的。

fromCat 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 catwhere 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列表中包含一個表達式以上的子查詢,你可以使用一個元組構造符(tupleconstructors):

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

注意在某些數據庫中(不包括OracleHSQL),你也可以在其他語境中使用元組構造符,比如查詢用戶類型的組件與組合:

from Person where name = ('Gavin', 'A', 'King')

該查詢等價於更複雜的:

from Person where name.first = 'Gavin' and name.initial = 'A' andname.last = 'King')



有兩個很好的理由使你不應當作這樣的事情:首先,它不完全適用於各個數據庫平台;其次,查詢現在依賴於映射文件中屬性的順序。

最後更新:2017-04-03 18:52:12

  上一篇:go java線程技術——線程的創建運行終止
  下一篇:go SQL Server判斷數據庫、表、存儲過程、函數是否存在