欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
HQL: Hibernate查詢(xún)語(yǔ)言
HQL: Hibernate查詢(xún)語(yǔ)言 Hibernate配備了一種非常強大的查詢(xún)語(yǔ)言,這種語(yǔ)言看上去很像SQL。但是不要被語(yǔ)法結構 上的相似所迷惑,HQL是非常有意識的被設計為完全面向對象的查詢(xún),它可以理解如繼承、多態(tài) 和關(guān)聯(lián)之類(lèi)的概念。
 
15.1. 大小寫(xiě)敏感性問(wèn)題
除了Java類(lèi)與屬性的名稱(chēng)外,查詢(xún)語(yǔ)句對大小寫(xiě)并不敏感。 所以 SeLeCT 與 sELEct 以及 SELECT 是相同的,但是 org.hibernate.eg.FOO 并不等價(jià)于 org.hibernate.eg.Foo 并且 foo.barSet 也不等價(jià)于 foo.BARSET。 
本手冊中的HQL關(guān)鍵字將使用小寫(xiě)字母. 很多用戶(hù)發(fā)現使用完全大寫(xiě)的關(guān)鍵字會(huì )使查詢(xún)語(yǔ)句 的可讀性更強, 但我們發(fā)現,當把查詢(xún)語(yǔ)句嵌入到Java語(yǔ)句中的時(shí)候使用大寫(xiě)關(guān)鍵字比較難看。 
15.2. from子句
Hibernate中最簡(jiǎn)單的查詢(xún)語(yǔ)句的形式如下: 
代碼內容
from eg.Cat 

該子句簡(jiǎn)單的返回eg.Cat類(lèi)的所有實(shí)例。 通常我們不需要使用類(lèi)的全限定名, 因為 auto-import(自動(dòng)引入) 是缺省的情況。 所以我們幾乎只使用如下的簡(jiǎn)單寫(xiě)法: 
代碼內容
from Cat 

大多數情況下, 你需要指定一個(gè)別名, 原因是你可能需要 在查詢(xún)語(yǔ)句的其它部分引用到Cat 
代碼內容
from Cat as cat 

這個(gè)語(yǔ)句把別名cat指定給類(lèi)Cat 的實(shí)例, 這樣我們就可以在隨后的查詢(xún)中使用此別名了。 關(guān)鍵字as 是可選的,我們也可以這樣寫(xiě): 
代碼內容
from Cat cat 

子句中可以同時(shí)出現多個(gè)類(lèi), 其查詢(xún)結果是產(chǎn)生一個(gè)笛卡兒積或產(chǎn)生跨表的連接。 
from Formula, Parameter
from Formula as form, Parameter as param
查詢(xún)語(yǔ)句中別名的開(kāi)頭部分小寫(xiě)被認為是實(shí)踐中的好習慣, 這樣做與Java變量的命名標準保持了一致 (比如,domesticCat)。 
15.3. 關(guān)聯(lián)(Association)與連接(Join)
我們也可以為相關(guān)聯(lián)的實(shí)體甚至是對一個(gè)集合中的全部元素指定一個(gè)別名, 這時(shí)要使用關(guān)鍵字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 

受支持的連接類(lèi)型是從ANSI SQL中借鑒來(lái)的。 
inner join(內連接) 
left outer join(左外連接) 
right outer join(右外連接) 
full join (全連接,并不常用) 
語(yǔ)句inner join, left outer join 以及 right outer join 可以簡(jiǎn)寫(xiě)。 
代碼內容
from Cat as cat  
    join cat.mate as mate 
    left join cat.kittens as kitten 

還有,一個(gè)"fetch"連接允許僅僅使用一個(gè)選擇語(yǔ)句就將相關(guān)聯(lián)的對象或一組值的集合隨著(zhù)他們的父對象的初始化而被初始化,這種方法在使用到集合的情況下尤其有用,對于關(guān)聯(lián)和集合來(lái)說(shuō),它有效的代替了映射文件中的外聯(lián)接 與延遲聲明(lazy declarations). 查看 第 20.1 節 “ 抓取策略(Fetching strategies) ” 以獲得等多的信息。 
代碼內容
from Cat as cat  
    inner join fetch cat.mate 
    left join fetch cat.kittens 

一個(gè)fetch連接通常不需要被指定別名, 因為相關(guān)聯(lián)的對象不應當被用在 where 子句 (或其它任何子句)中。同時(shí),相關(guān)聯(lián)的對象 并不在查詢(xún)的結果中直接返回,但可以通過(guò)他們的父對象來(lái)訪(fǎng)問(wèn)到他們。 
注意fetch構造變量在使用了scroll() 或 iterate()函數 的查詢(xún)中是不能使用的。最后注意,使用full join fetch 與 right join fetch是沒(méi)有意義的。 
如果你使用屬性級別的延遲獲?。╨azy fetching)(這是通過(guò)重新編寫(xiě)字節碼實(shí)現的),可以使用 fetch all properties 來(lái)強制Hibernate立即取得那些原本需要延遲加載的屬性(在第一個(gè)查詢(xún)中)。 
代碼內容
from Document fetch all properties order by name 
from Document doc fetch all properties where lower(doc.name) like ’%cats%’
15.4. select子句
select 子句選擇將哪些對象與屬性返 回到查詢(xún)結果集中. 考慮如下情況: 
代碼內容
select mate  
from Cat as cat  
    inner join cat.mate as mate 
該語(yǔ)句將選擇mates of other Cats。(其他貓的配偶) 實(shí)際上, 你可以更簡(jiǎn)潔的用以下的查詢(xún)語(yǔ)句表達相同的含義: 
代碼內容
select cat.mate from Cat cat 

查詢(xún)語(yǔ)句可以返回值為任何類(lèi)型的屬性,包括返回類(lèi)型為某種組件(Component)的屬性: 
代碼內容
select cat.name from DomesticCat cat 
where cat.name like ’fri%’
 select cust.name.firstName from Customer as cust

查詢(xún)語(yǔ)句可以返回多個(gè)對象和(或)屬性,存放在 Object[]隊列中, 
代碼內容
select mother, offspr, mate.name  
from DomesticCat as mother 
    inner join mother.mate as mate 
    left outer join mother.kittens as offspr 

或存放在一個(gè)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 

也可能直接返回一個(gè)實(shí)際的類(lèi)型安全的Java對象, 
代碼內容
select new Family(mother, mate, offspr) 
from DomesticCat as mother 
    join mother.mate as mate 
    left join mother.kittens as offspr 
假設類(lèi)Family有一個(gè)合適的構造函數. 
你可以使用關(guān)鍵字as給“被選擇了的表達式”指派別名: 
代碼內容
select max(bodyWeight) as max, min(bodyWeight) as min, count(*) as n 
from Cat cat 

這種做法在與子句select new map一起使用時(shí)最有用: 
代碼內容
select new map( max(bodyWeight) as max, min(bodyWeight) as min, count(*) as n ) 
from Cat cat 

該查詢(xún)返回了一個(gè)Map的對象,內容是別名與被選擇的值組成的名-值映射。 

15.5. 聚集函數
HQL查詢(xún)甚至可以返回作用于屬性之上的聚集函數的計算結果: 
代碼內容
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...)  

你可以在選擇子句中使用數學(xué)操作符、連接以及經(jīng)過(guò)驗證的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 

關(guān)鍵字distinct與all 也可以使用,它們具有與SQL相同的語(yǔ)義. 
代碼內容
select distinct cat.name from Cat cat 
select count(distinct cat.name), count(cat) from Cat cat


15.6. 多態(tài)查詢(xún)
一個(gè)如下的查詢(xún)語(yǔ)句: 
代碼內容
from Cat as cat 

不僅返回Cat類(lèi)的實(shí)例, 也同時(shí)返回子類(lèi) DomesticCat的實(shí)例. Hibernate 可以在from子句中指定任何 Java 類(lèi)或接口. 查詢(xún)會(huì )返回繼承了該類(lèi)的所有持久化子類(lèi) 的實(shí)例或返回聲明了該接口的所有持久化類(lèi)的實(shí)例。下面的查詢(xún)語(yǔ)句返回所有的被持久化的對象: 
代碼內容
from java.lang.Object o 

接口Named 可能被各種各樣的持久化類(lèi)聲明: 
代碼內容
from Named n, Named m where n.name = m.name 

注意,最后的兩個(gè)查詢(xún)將需要超過(guò)一個(gè)的SQL SELECT.這表明order by子句 沒(méi)有對整個(gè)結果集進(jìn)行正確的排序. (這也說(shuō)明你不能對這樣的查詢(xún)使用Query.scroll()方法.) 

15.7. where子句
where子句允許你將返回的實(shí)例列表的范圍縮小. 如果沒(méi)有指定別名,你可以使用屬性名來(lái)直接引用屬性: 
代碼內容
from Cat where name=’Fritz’ 
如果指派了別名,需要使用完整的屬性名: 
代碼內容
from Cat as cat where cat.name=’Fritz’ 
返回名為(屬性name等于)’Fritz’的Cat類(lèi)的實(shí)例。 
代碼內容
select foo  
from Foo foo, Bar bar 
where foo.startDate = bar.date 

將返回所有滿(mǎn)足下面條件的Foo類(lèi)的實(shí)例: 存在如下的bar的一個(gè)實(shí)例,其date屬性等于 Foo的startDate屬性。 復合路徑表達式使得where子句非常的強大,考慮如下情況: 
null
該查詢(xún)將被翻譯成為一個(gè)含有表連接(內連接)的SQL查詢(xún)。如果你打算寫(xiě)像這樣的查詢(xún)語(yǔ)句 
代碼內容
from Foo foo   
where foo.bar.baz.customer.address.city is not null 
在SQL中,你為達此目的將需要進(jìn)行一個(gè)四表連接的查詢(xún)。 
=運算符不僅可以被用來(lái)比較屬性的值,也可以用來(lái)比較實(shí)例: 
代碼內容
from Cat cat, Cat rival where cat.mate = rival.mate 
select cat, mate  
from Cat cat, Cat mate 
where cat.mate = mate 

特殊屬性(小寫(xiě))id可以用來(lái)表示一個(gè)對象的唯一的標識符。(你也可以使用該對象的屬性名。) 
代碼內容
from Cat as cat where cat.id = 123 

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

第二個(gè)查詢(xún)是有效的。此時(shí)不需要進(jìn)行表連接! 
同樣也可以使用復合標識符。比如Person類(lèi)有一個(gè)復合標識符,它由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 

第二個(gè)查詢(xún)也不需要進(jìn)行表連接。 
同樣的,特殊屬性class在進(jìn)行多態(tài)持久化的情況下被用來(lái)存取一個(gè)實(shí)例的鑒別值(discriminator value)。 一個(gè)嵌入到where子句中的Java類(lèi)的名字將被轉換為該類(lèi)的鑒別值。 
代碼內容
from Cat cat where cat.class = DomesticCat 

你也可以聲明一個(gè)屬性的類(lèi)型是組件或者復合用戶(hù)類(lèi)型(以及由組件構成的組件等等)。永遠不要嘗試使用以組件類(lèi)型來(lái)結尾的路徑表達式(path-expression) (與此相反,你應當使用組件的一個(gè)屬性來(lái)結尾)。 舉例來(lái)說(shuō),如果store.owner含有一個(gè)包含了組件的實(shí)體address 
代碼內容
store.owner.address.city    // 正確 
store.owner.address         // 錯誤! 

一個(gè)“任意”類(lèi)型有兩個(gè)特殊的屬性id和class, 來(lái)允許我們按照下面的方式表達一個(gè)連接(AuditLog.item 是一個(gè)屬性,該屬性被映射為<any>)。 
代碼內容
from AuditLog log, Payment payment  
where log.item.class = ’Payment’ and log.item.id = payment.id 
注意,在上面的查詢(xún)與句中,log.item.class 和 payment.class 將涉及到完全不同的數據庫中的列。 

15.8. 表達式
在where子句中允許使用的表達式包括 大多數你可以在SQL使用的表達式種類(lèi): 
數學(xué)運算符+, -, *, / 
二進(jìn)制比較運算符=, >=, <=, <>, !=, like 
邏輯運算符and, or, not 
in, not in, between, is null, is not null, is empty, is not empty, member of and not member of 
"簡(jiǎn)單的" 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 ...), 其第二個(gè)參數是某Hibernate類(lèi)型的名字,以及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 類(lèi)型的常量 eg.Color.TABBY 
關(guān)鍵字in與between可按如下方法使用: 
代碼內容
from DomesticCat cat where cat.name between ’A’ and ’B’ 
from DomesticCat cat where cat.name in ( ’Foo’, ’Bar’, ’Baz’ ) 

而且否定的格式也可以如下書(shū)寫(xiě): 
代碼內容
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可以被用來(lái)測試空值(null). 
在Hibernate配置文件中聲明HQL“查詢(xún)替代(query substitutions)”之后, 布爾表達式(Booleans)可以在其他表達式中輕松的使用: 
代碼內容
<property name="hibernate.query.substitutions">true 1, false 0</property> 

系統將該HQL轉換為SQL語(yǔ)句時(shí),該設置表明將用字符 1 和 0 來(lái) 取代關(guān)鍵字true 和 false: 
代碼內容
from Cat cat where cat.alive = true 
你可以用特殊屬性size, 或是特殊函數size()測試一個(gè)集合的大小。 
代碼內容
from Cat cat where cat.kittens.size > 0 
from Cat cat where size(cat.kittens) > 0 

對于索引了(有序)的集合,你可以使用minindex 與 maxindex函數來(lái)引用到最小與最大的索引序數。 同理,你可以使用minelement 與 maxelement函數來(lái) 引用到一個(gè)基本數據類(lèi)型的集合中最小與最大的元素。 
代碼內容
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 

在傳遞一個(gè)集合的索引集或者是元素集(elements與indices 函數) 或者傳遞一個(gè)子查詢(xún)的結果的時(shí)候,可以使用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子句中使用。 
一個(gè)被索引過(guò)的(有序的)集合的元素(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 
在[]中的表達式甚至可以是一個(gè)算數表達式。  
select item from Item item, Order order 
where order.items[ size(order.items) - 1 ] = item 

對于一個(gè)一對多的關(guān)聯(lián)(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%’
如果你還不能對所有的這些深信不疑,想想下面的查詢(xún)。如果使用SQL,語(yǔ)句長(cháng)度會(huì )增長(cháng)多少,可讀性會(huì )下降多少: 
代碼內容
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) 
提示: 會(huì )像如下的語(yǔ)句  
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 
    )
15.9. order by子句
查詢(xún)返回的列表(list)可以按照一個(gè)返回的類(lèi)或組件(components)中的任何屬性(property)進(jìn)行排序: 
代碼內容
from DomesticCat cat 
order by cat.name asc, cat.weight desc, cat.birthdate 

可選的asc或desc關(guān)鍵字指明了按照升序或降序進(jìn)行排序. 

15.10. group by子句
一個(gè)返回聚集值(aggregate values)的查詢(xún)可以按照一個(gè)返回的類(lèi)或組件(components)中的任何屬性(property)進(jìn)行分組: 
代碼內容
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) 
如果底層的數據庫支持的話(huà)(例如不能在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子句中都不能包含算術(shù)表達式(arithmetic expressions). 
15.11. 子查詢(xún)
對于支持子查詢(xún)的數據庫,Hibernate支持在查詢(xún)中使用子查詢(xún)。一個(gè)子查詢(xún)必須被圓括號包圍起來(lái)(經(jīng)常是SQL聚集函數的圓括號)。 甚至相互關(guān)聯(lián)的子查詢(xún)(引用到外部查詢(xún)中的別名的子查詢(xún))也是允許的。 
代碼內容
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列表中包含一個(gè)表達式以上的子查詢(xún),你可以使用一個(gè)元組構造符(tuple constructors): 
代碼內容
from Cat as cat  
where not ( cat.name, cat.color ) in (  
    select cat.name, cat.color from DomesticCat cat  
注意在某些數據庫中(不包括Oracle與HSQL),你也可以在其他語(yǔ)境中使用元組構造符, 比如查詢(xún)用戶(hù)類(lèi)型的組件與組合: 
代碼內容
from Person where name = (’Gavin’, ’A’, ’King’) 

該查詢(xún)等價(jià)于更復雜的: 
代碼內容
from Person where name.first = ’Gavin’ and name.initial = ’A’ and name.last = ’King’) 

有兩個(gè)很好的理由使你不應當作這樣的事情:首先,它不完全適用于各個(gè)數據庫平臺;其次,查詢(xún)現在依賴(lài)于映射文件中屬性的順序。 
15.12. HQL示例
Hibernate查詢(xún)可以非常的強大與復雜。實(shí)際上,Hibernate的一個(gè)主要賣(mài)點(diǎn)就是查詢(xún)語(yǔ)句的威力。這里有一些例子,它們與我在最近的 一個(gè)項目中使用的查詢(xún)非常相似。注意你能用到的大多數查詢(xún)比這些要簡(jiǎn)單的多! 
下面的查詢(xún)對于某個(gè)特定的客戶(hù)的所有未支付的賬單,在給定給最小總價(jià)值的情況下,返回訂單的id,條目的數量和總價(jià)值, 返回值按照總價(jià)值的結果進(jìn)行排序。為了決定價(jià)格,查詢(xún)使用了當前目錄。作為轉換結果的SQL查詢(xún),使用了ORDER, ORDER_LINE, PRODUCT, CATALOG 和PRICE 庫表。 
代碼內容
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 

這簡(jiǎn)直是一個(gè)怪物!實(shí)際上,在現實(shí)生活中,我并不熱衷于子查詢(xún),所以我的查詢(xún)語(yǔ)句看起來(lái)更像這個(gè): 
代碼內容
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 
下面一個(gè)查詢(xún)計算每一種狀態(tài)下的支付的數目,除去所有處于A(yíng)WAITING_APPROVAL狀態(tài)的支付,因為在該狀態(tài)下 當前的用戶(hù)作出了狀態(tài)的最新改變。該查詢(xún)被轉換成含有兩個(gè)內連接以及一個(gè)相關(guān)聯(lián)的子選擇的SQL查詢(xún),該查詢(xún)使用了表 PAYMENT, PAYMENT_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實(shí)例集映射為一個(gè)列表(list)而不是一個(gè)集合(set), 書(shū)寫(xiě)查詢(xún)語(yǔ)句將更加簡(jiǎn)單. 
代碼內容
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 
下面一個(gè)查詢(xún)使用了MS SQL Server的 isNull()函數用以返回當前用戶(hù)所屬組織的組織賬號及組織未支付的賬。 它被轉換成一個(gè)對表ACCOUNT, PAYMENT, PAYMENT_STATUS, ACCOUNT_TYPE, ORGANIZATION 以及 ORG_USER進(jìn)行的三個(gè)內連接, 一個(gè)外連接和一個(gè)子選擇的SQL查詢(xún)。 
代碼內容
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 
對于一些數據庫,我們需要棄用(相關(guān)的)子選擇。 
代碼內容
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 
15.13. 批量的UPDATE & DELETE語(yǔ)句
HQL現在支持UPDATE與DELETE語(yǔ)句. 查閱 第 14.3 節 “大批量更新/刪除(Bulk update/delete)” 以獲得更多信息。 
15.14. 小技巧 & 小竅門(mén)
你可以統計查詢(xún)結果的數目而不必實(shí)際的返回他們: 
( (Integer) session.iterate("select count(*) from ....").next() ).intValue()
若想根據一個(gè)集合的大小來(lái)進(jìn)行排序,可以使用如下的語(yǔ)句: 
代碼內容
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) 

如果你的數據庫支持子選擇,你可以在你的查詢(xún)的where子句中為選擇的大?。╯election size)指定一個(gè)條件: 
代碼內容
from User usr where size(usr.messages) >= 1 

如果你的數據庫不支持子選擇語(yǔ)句,使用下面的查詢(xún): 
代碼內容
select usr.id, usr.name 
from User usr.name 
    join usr.messages msg 
group by usr.id, usr.name 
having count(msg) >= 1 
因為內連接(inner join)的原因,這個(gè)解決方案不能返回含有零個(gè)信息的User 類(lèi)的實(shí)例, 所以這種情況下使用下面的格式將是有幫助的: 
代碼內容
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的屬性可以被綁定到一個(gè)命名查詢(xún)(named query)的參數上: 
代碼內容
Query q = s.createQuery("from foo Foo as foo where foo.name=:name and foo.size=:size"); 
q.setProperties(fooBean); // fooBean包含方法getName()與getSize() 
List foos = q.list(); 

通過(guò)將接口Query與一個(gè)過(guò)濾器(filter)一起使用,集合(Collections)是可以分頁(yè)的: 
代碼內容
Query q = s.createFilter( collection, "" ); // 一個(gè)簡(jiǎn)單的過(guò)濾器 
q.setMaxResults(PAGE_SIZE); 
q.setFirstResult(PAGE_SIZE * pageNumber); 
List page = q.list(); 

通過(guò)使用查詢(xún)過(guò)濾器(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" ); 
不用通過(guò)初始化,你就可以知道一個(gè)集合(Collection)的大?。?nbsp;
代碼內容
( (Integer) session.iterate("select count(*) from ....").next() ).intValue();
本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
第?16?章?HQL: Hibernate 查詢(xún)語(yǔ)言
求教!用hibernate,怎么用count(),sum(),怎么能根據條件查詢(xún),執行語(yǔ)句? Java / 框架、開(kāi)源
saveOrupdate與update的作用(講得太清楚了)
Hibernate4實(shí)戰 之 第三部分:Hibernate的基本開(kāi)發(fā)
mysql的查詢(xún)、子查詢(xún)及連接查詢(xún)
Join用法,HQL的方法,Hibernate中的fetch
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久