HQL語句大全(轉(zhuǎn)) 第?15?章?HQL: Hibernate查詢語言 Hibernate配備了一種非常強(qiáng)大的查詢語言,這種語言看上去很像SQL。但是不要被語法結(jié)構(gòu) 上的相似所迷惑,HQL是非常有意識的被設(shè)計(jì)為完全面向?qū)ο蟮牟樵?#xff0c;它可以理解如繼承、多態(tài) 和關(guān)聯(lián)之類的概念。 15.1.?大小寫敏感性問題 除了Java類與屬性的名稱外,查詢語句對大小寫并不敏感。 所以?SeLeCT?與?sELEct?以及?SELECT?是相同的,但是?org.hibernate.eg.FOO?并不等價于?org.hibernate.eg.Foo?并且?foo.barSet?也不等價于?foo.BARSET。 本手冊中的HQL關(guān)鍵字將使用小寫字母. 很多用戶發(fā)現(xiàn)使用完全大寫的關(guān)鍵字會使查詢語句 的可讀性更強(qiáng), 但我們發(fā)現(xiàn),當(dāng)把查詢語句嵌入到Java語句中的時候使用大寫關(guān)鍵字比較難看。 15.2.?from子句 Hibernate中最簡單的查詢語句的形式如下: from eg.Cat 該子句簡單的返回eg.Cat類的所有實(shí)例。 通常我們不需要使用類的全限定名, 因?yàn)?auto-import(自動引入) 是缺省的情況。 所以我們幾乎只使用如下的簡單寫法: from Cat 大多數(shù)情況下, 你需要指定一個別名, 原因是你可能需要 在查詢語句的其它部分引用到Cat from Cat as cat 這個語句把別名cat指定給類Cat?的實(shí)例, 這樣我們就可以在隨后的查詢中使用此別名了。 關(guān)鍵字as?是可選的,我們也可以這樣寫: from Cat cat 子句中可以同時出現(xiàn)多個類, 其查詢結(jié)果是產(chǎn)生一個笛卡兒積或產(chǎn)生跨表的連接。 from Formula, Parameterfrom Formula as form, Parameter as param 查詢語句中別名的開頭部分小寫被認(rèn)為是實(shí)踐中的好習(xí)慣, 這樣做與Java變量的命名標(biāo)準(zhǔn)保持了一致 (比如,domesticCat)。 15.3.?關(guān)聯(lián)(Association)與連接(Join) 我們也可以為相關(guān)聯(lián)的實(shí)體甚至是對一個集合中的全部元素指定一個別名, 這時要使用關(guān)鍵字join。 from Cat as cat inner join cat.mate as mate left outer join cat.kittens as kittenfrom Cat as cat left join cat.mate.kittens as kittensfrom Formula form full join form.parameter param 受支持的連接類型是從ANSI SQL中借鑒來的。 -
inner join(內(nèi)連接) -
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"連接允許僅僅使用一個選擇語句就將相關(guān)聯(lián)的對象或一組值的集合隨著他們的父對象的初始化而被初始化,這種方法在使用到集合的情況下 尤其有用,對于關(guān)聯(lián)和集合來說,它有效的代替了映射文件中的外聯(lián)接 與延遲聲明(lazy declarations). 查看?第?20.1?節(jié) “ 抓取策略(Fetching strategies) ”?以獲得等多的信息。 from Cat as cat inner join fetch cat.mate left join fetch cat.kittens 一個fetch連接通常不需要被指定別名, 因?yàn)橄嚓P(guān)聯(lián)的對象不應(yīng)當(dāng)被用在?where?子句 (或其它任何子句)中。同時,相關(guān)聯(lián)的對象 并不在查詢的結(jié)果中直接返回,但可以通過他們的父對象來訪問到他們。 注意fetch構(gòu)造變量在使用了scroll()?或?iterate()函數(shù) 的查詢中是不能使用的。最后注意,使用full join fetch?與?right join fetch是沒有意義的。 如果你使用屬性級別的延遲獲取(lazy fetching)(這是通過重新編寫字節(jié)碼實(shí)現(xiàn)的),可以使用?fetch all properties?來強(qiáng)制Hibernate立即取得那些原本需要延遲加載的屬性(在第一個查詢中)。 from Document fetch all properties order by namefrom Document doc fetch all properties where lower(doc.name) like '%cats%' 15.4.?select子句 select?子句選擇將哪些對象與屬性返 回到查詢結(jié)果集中. 考慮如下情況: select mate from Cat as cat inner join cat.mate as mate 該語句將選擇mates of other?Cats。(其他貓的配偶) 實(shí)際上, 你可以更簡潔的用以下的查詢語句表達(dá)相同的含義: select cat.mate from Cat cat 查詢語句可以返回值為任何類型的屬性,包括返回類型為某種組件(Component)的屬性: select cat.name from DomesticCat catwhere cat.name like 'fri%'select cust.name.firstName from Customer as cust 查詢語句可以返回多個對象和(或)屬性,存放在?Object[]隊(duì)列中, 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 也可能直接返回一個實(shí)際的類型安全的Java對象, select new Family(mother, mate, offspr)from DomesticCat as mother join mother.mate as mate left join mother.kittens as offspr 假設(shè)類Family有一個合適的構(gòu)造函數(shù). 你可以使用關(guān)鍵字as給“被選擇了的表達(dá)式”指派別名: select max(bodyWeight) as max, min(bodyWeight) as min, count(*) as nfrom Cat cat 這種做法在與子句select new map一起使用時最有用: select new map( max(bodyWeight) as max, min(bodyWeight) as min, count(*) as n )from Cat cat 該查詢返回了一個Map的對象,內(nèi)容是別名與被選擇的值組成的名-值映射。 15.5.?聚集函數(shù) HQL查詢甚至可以返回作用于屬性之上的聚集函數(shù)的計(jì)算結(jié)果: select avg(cat.weight), sum(cat.weight), max(cat.weight), count(cat)from Cat cat 受支持的聚集函數(shù)如下: -
avg(...), sum(...), min(...), max(...) -
count(*) -
count(...), count(distinct ...), count(all...) 你可以在選擇子句中使用數(shù)學(xué)操作符、連接以及經(jīng)過驗(yàn)證的SQL函數(shù): select cat.weight + sum(kitten.weight) from Cat cat join cat.kittens kittengroup by cat.id, cat.weightselect firstName||' '||initial||' '||upper(lastName) from Person 關(guān)鍵字distinct與all?也可以使用,它們具有與SQL相同的語義. select distinct cat.name from Cat catselect count(distinct cat.name), count(cat) from Cat cat 15.6.?多態(tài)查詢 一個如下的查詢語句: from Cat as cat 不僅返回Cat類的實(shí)例, 也同時返回子類?DomesticCat的實(shí)例. Hibernate 可以在from子句中指定任何?Java 類或接口. 查詢會返回繼承了該類的所有持久化子類 的實(shí)例或返回聲明了該接口的所有持久化類的實(shí)例。下面的查詢語句返回所有的被持久化的對象: from java.lang.Object o 接口Named?可能被各種各樣的持久化類聲明: from Named n, Named m where n.name = m.name 注意,最后的兩個查詢將需要超過一個的SQL?SELECT.這表明order by子句 沒有對整個結(jié)果集進(jìn)行正確的排序. (這也說明你不能對這樣的查詢使用Query.scroll()方法.) 15.7.?where子句 where子句允許你將返回的實(shí)例列表的范圍縮小. 如果沒有指定別名,你可以使用屬性名來直接引用屬性: from Cat where name='Fritz' 如果指派了別名,需要使用完整的屬性名: from Cat as cat where cat.name='Fritz' 返回名為(屬性name等于)'Fritz'的Cat類的實(shí)例。 select foo from Foo foo, Bar barwhere foo.startDate = bar.date 將返回所有滿足下面條件的Foo類的實(shí)例: 存在如下的bar的一個實(shí)例,其date屬性等于?Foo的startDate屬性。 復(fù)合路徑表達(dá)式使得where子句非常的強(qiáng)大,考慮如下情況: from Cat cat where cat.mate.name is not null 該查詢將被翻譯成為一個含有表連接(內(nèi)連接)的SQL查詢。如果你打算寫像這樣的查詢語句 from Foo foo where foo.bar.baz.customer.address.city is not null 在SQL中,你為達(dá)此目的將需要進(jìn)行一個四表連接的查詢。 =運(yùn)算符不僅可以被用來比較屬性的值,也可以用來比較實(shí)例: from Cat cat, Cat rival where cat.mate = rival.mateselect cat, mate from Cat cat, Cat matewhere cat.mate = mate 特殊屬性(小寫)id可以用來表示一個對象的唯一的標(biāo)識符。(你也可以使用該對象的屬性名。) from Cat as cat where cat.id = 123from Cat as cat where cat.mate.id = 69 第二個查詢是有效的。此時不需要進(jìn)行表連接! 同樣也可以使用復(fù)合標(biāo)識符。比如Person類有一個復(fù)合標(biāo)識符,它由country屬性 與medicareNumber屬性組成。 from bank.Person personwhere person.id.country = 'AU' and person.id.medicareNumber = 123456from bank.Account accountwhere account.owner.id.country = 'AU' and account.owner.id.medicareNumber = 123456 第二個查詢也不需要進(jìn)行表連接。 同樣的,特殊屬性class在進(jìn)行多態(tài)持久化的情況下被用來存取一個實(shí)例的鑒別值(discriminator value)。 一個嵌入到where子句中的Java類的名字將被轉(zhuǎn)換為該類的鑒別值。 from Cat cat where cat.class = DomesticCat 你也可以聲明一個屬性的類型是組件或者復(fù)合用戶類型(以及由組件構(gòu)成的組件等等)。永遠(yuǎn)不要嘗試使用以組件類型來結(jié)尾的路徑表達(dá)式(path-expression) (與此相反,你應(yīng)當(dāng)使用組件的一個屬性來結(jié)尾)。 舉例來說,如果store.owner含有一個包含了組件的實(shí)體address store.owner.address.city // 正確store.owner.address // 錯誤! 一個“任意”類型有兩個特殊的屬性id和class, 來允許我們按照下面的方式表達(dá)一個連接(AuditLog.item?是一個屬性,該屬性被映射為<any>)。 from AuditLog log, Payment payment where log.item.class = 'Payment' and log.item.id = payment.id 注意,在上面的查詢與句中,log.item.class?和?payment.class?將涉及到完全不同的數(shù)據(jù)庫中的列。 15.8.?表達(dá)式 在where子句中允許使用的表達(dá)式包括 大多數(shù)你可以在SQL使用的表達(dá)式種類: -
數(shù)學(xué)運(yùn)算符+, -, *, / -
二進(jìn)制比較運(yùn)算符=, >=, <=, <>, !=, like -
邏輯運(yùn)算符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定義的任何函數(shù)或操作:substring(), trim(), lower(), upper(), length(), locate(), abs(), sqrt(), bit_length() -
coalesce()?和?nullif() -
cast(... as ...), 其第二個參數(shù)是某Hibernate類型的名字,以及extract(... from ...),只要ANSI?cast()?和extract()?被底層數(shù)據(jù)庫支持 -
任何數(shù)據(jù)庫支持的SQL標(biāo)量函數(shù),比如sign(),?trunc(),?rtrim(),?sin() -
JDBC參數(shù)傳入?? -
命名參數(shù):name,?:start_date,?:x1 -
SQL 直接常量?'foo',?69,?'1970-01-01 10:00:01.0' -
Java?public static final?類型的常量?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' ) 而且否定的格式也可以如下書寫: 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)”之后, 布爾表達(dá)式(Booleans)可以在其他表達(dá)式中輕松的使用: <property name="hibernate.query.substitutions">true 1, false 0</property> 系統(tǒng)將該HQL轉(zhuǎn)換為SQL語句時,該設(shè)置表明將用字符?1?和?0?來 取代關(guān)鍵字true?和?false: from Cat cat where cat.alive = true 你可以用特殊屬性size, 或是特殊函數(shù)size()測試一個集合的大小。 from Cat cat where cat.kittens.size > 0from Cat cat where size(cat.kittens) > 0 對于索引了(有序)的集合,你可以使用minindex?與?maxindex函數(shù)來引用到最小與最大的索引序數(shù)。 同理,你可以使用minelement?與?maxelement函數(shù)來 引用到一個基本數(shù)據(jù)類型的集合中最小與最大的元素。 from Calendar cal where maxelement(cal.holidays) > current datefrom Order order where maxindex(order.items) > 100from Order order where minelement(order.items) > 10000 在傳遞一個集合的索引集或者是元素集(elements與indices?函數(shù)) 或者傳遞一個子查詢的結(jié)果的時候,可以使用SQL函數(shù)any, some, all, exists, in select mother from Cat as mother, Cat as kitwhere kit in elements(foo.kittens)select p from NameList list, Person pwhere 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種,這些結(jié)構(gòu)變量-?size,?elements,?indices,?minindex,?maxindex,?minelement,?maxelement?- 只能在where子句中使用。 一個被索引過的(有序的)集合的元素(arrays, lists, maps)可以在其他索引中被引用(只能在where子句中): from Order order where order.items[0].id = 1234select person from Person person, Calendar calendarwhere calendar.holidays['national day'] = person.birthDay and person.nationality.calendar = calendarselect item from Item item, Order orderwhere order.items[ order.deliveredItemIndices[0] ] = item and order.id = 11select item from Item item, Order orderwhere order.items[ maxindex(order.items) ] = item and order.id = 11 在[]中的表達(dá)式甚至可以是一個算數(shù)表達(dá)式。 select item from Item item, Order orderwhere order.items[ size(order.items) - 1 ] = item 對于一個一對多的關(guān)聯(lián)(one-to-many association)或是值的集合中的元素, HQL也提供內(nèi)建的index()函數(shù), select item, index(item) from Order order join order.items itemwhere index(item) < 5 如果底層數(shù)據(jù)庫支持標(biāo)量的SQL函數(shù),它們也可以被使用 from DomesticCat cat where upper(cat.name) like 'FRI%' 如果你還不能對所有的這些深信不疑,想想下面的查詢。如果使用SQL,語句長度會增長多少,可讀性會下降多少: select custfrom Product prod, Store store inner join store.customers custwhere 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_orderFROM customers cust, stores store, locations loc, store_customers sc, product prodWHERE 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子句 查詢返回的列表(list)可以按照一個返回的類或組件(components)中的任何屬性(property)進(jìn)行排序: from DomesticCat catorder by cat.name asc, cat.weight desc, cat.birthdate 可選的asc或desc關(guān)鍵字指明了按照升序或降序進(jìn)行排序. 15.10.?group by子句 一個返回聚集值(aggregate values)的查詢可以按照一個返回的類或組件(components)中的任何屬性(property)進(jìn)行分組: select cat.color, sum(cat.weight), count(cat) from Cat catgroup by cat.colorselect foo.id, avg(name), max(name) from Foo foo join foo.names namegroup by foo.id having子句在這里也允許使用. select cat.color, sum(cat.weight), count(cat) from Cat catgroup by cat.color having cat.color in (eg.Color.TABBY, eg.Color.BLACK) 如果底層的數(shù)據(jù)庫支持的話(例如不能在MySQL中使用),SQL的一般函數(shù)與聚集函數(shù)也可以出現(xiàn) 在having與order by?子句中。 select catfrom Cat cat join cat.kittens kittengroup by cathaving avg(kitten.weight) > 100order by count(kitten) asc, sum(kitten.weight) desc 注意group by子句與?order by子句中都不能包含算術(shù)表達(dá)式(arithmetic expressions). 15.11.?子查詢 對于支持子查詢的數(shù)據(jù)庫,Hibernate支持在查詢中使用子查詢。一個子查詢必須被圓括號包圍起來(經(jīng)常是SQL聚集函數(shù)的圓括號)。 甚至相互關(guān)聯(liá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列表中包含一個表達(dá)式以上的子查詢,你可以使用一個元組構(gòu)造符(tuple constructors): from Cat as cat where not ( cat.name, cat.color ) in ( select cat.name, cat.color from DomesticCat cat ) 注意在某些數(shù)據(jù)庫中(不包括Oracle與HSQL),你也可以在其他語境中使用元組構(gòu)造符, 比如查詢用戶類型的組件與組合: from Person where name = ('Gavin', 'A', 'King') 該查詢等價于更復(fù)雜的: from Person where name.first = 'Gavin' and name.initial = 'A' and name.last = 'King') 有兩個很好的理由使你不應(yīng)當(dāng)作這樣的事情:首先,它不完全適用于各個數(shù)據(jù)庫平臺;其次,查詢現(xiàn)在依賴于映射文件中屬性的順序。 15.12.?HQL示例 Hibernate查詢可以非常的強(qiáng)大與復(fù)雜。實(shí)際上,Hibernate的一個主要賣點(diǎn)就是查詢語句的威力。這里有一些例子,它們與我在最近的 一個項(xiàng)目中使用的查詢非常相似。注意你能用到的大多數(shù)查詢比這些要簡單的多! 下面的查詢對于某個特定的客戶的所有未支付的賬單,在給定給最小總價值的情況下,返回訂單的id,條目的數(shù)量和總價值, 返回值按照總價值的結(jié)果進(jìn)行排序。為了決定價格,查詢使用了當(dāng)前目錄。作為轉(zhuǎn)換結(jié)果的SQL查詢,使用了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 pricewhere 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 orderhaving sum(price.amount) > :minAmountorder by sum(price.amount) desc 這簡直是一個怪物!實(shí)際上,在現(xiàn)實(shí)生活中,我并不熱衷于子查詢,所以我的查詢語句看起來更像這個: 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 pricewhere order.paid = false and order.customer = :customer and price.product = product and catalog = :currentCataloggroup by orderhaving sum(price.amount) > :minAmountorder by sum(price.amount) desc 下面一個查詢計(jì)算每一種狀態(tài)下的支付的數(shù)目,除去所有處于AWAITING_APPROVAL狀態(tài)的支付,因?yàn)樵谠摖顟B(tài)下 當(dāng)前的用戶作出了狀態(tài)的最新改變。該查詢被轉(zhuǎn)換成含有兩個內(nèi)連接以及一個相關(guān)聯(lián)的子選擇的SQL查詢,該查詢使用了表?PAYMENT,?PAYMENT_STATUS?以及?PAYMENT_STATUS_CHANGE。 select count(payment), status.name from Payment as payment join payment.currentStatus as status join payment.statusChanges as statusChangewhere 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.sortOrderorder by status.sortOrder 如果我把statusChanges實(shí)例集映射為一個列表(list)而不是一個集合(set), 書寫查詢語句將更加簡單. select count(payment), status.name from Payment as payment join payment.currentStatus as statuswhere payment.status.name <> PaymentStatus.AWAITING_APPROVAL or payment.statusChanges[ maxIndex(payment.statusChanges) ].user <> :currentUsergroup by status.name, status.sortOrderorder by status.sortOrder 下面一個查詢使用了MS SQL Server的?isNull()函數(shù)用以返回當(dāng)前用戶所屬組織的組織帳號及組織未支付的賬。 它被轉(zhuǎn)換成一個對表ACCOUNT,?PAYMENT,?PAYMENT_STATUS,?ACCOUNT_TYPE,?ORGANIZATION?以及?ORG_USER進(jìn)行的三個內(nèi)連接, 一個外連接和一個子選擇的SQL查詢。 select account, paymentfrom Account as account left outer join account.payments as paymentwhere :currentUser in elements(account.holder.users) and PaymentStatus.UNPAID = isNull(payment.currentStatus.name, PaymentStatus.UNPAID)order by account.type.sortOrder, account.accountNumber, payment.dueDate 對于一些數(shù)據(jù)庫,我們需要棄用(相關(guān)的)子選擇。 select account, paymentfrom Account as account join account.holder.users as user left outer join account.payments as paymentwhere :currentUser = user and PaymentStatus.UNPAID = isNull(payment.currentStatus.name, PaymentStatus.UNPAID)order by account.type.sortOrder, account.accountNumber, payment.dueDate 15.13.?批量的UPDATE & DELETE語句 HQL現(xiàn)在支持UPDATE與DELETE語句. 查閱?第?14.3?節(jié) “大批量更新/刪除(Bulk update/delete)”?以獲得更多信息。 15.14.?小技巧 & 小竅門 你可以統(tǒng)計(jì)查詢結(jié)果的數(shù)目而不必實(shí)際的返回他們: ( (Integer) session.iterate("select count(*) from ....").next() ).intValue() 若想根據(jù)一個集合的大小來進(jìn)行排序,可以使用如下的語句: select usr.id, usr.namefrom User as usr left join usr.messages as msggroup by usr.id, usr.nameorder by count(msg) 如果你的數(shù)據(jù)庫支持子選擇,你可以在你的查詢的where子句中為選擇的大小(selection size)指定一個條件: from User usr where size(usr.messages) >= 1 如果你的數(shù)據(jù)庫不支持子選擇語句,使用下面的查詢: select usr.id, usr.namefrom User usr.name join usr.messages msggroup by usr.id, usr.namehaving count(msg) >= 1 因?yàn)閮?nèi)連接(inner join)的原因,這個解決方案不能返回含有零個信息的User?類的實(shí)例, 所以這種情況下使用下面的格式將是有幫助的: select usr.id, usr.namefrom User as usr left join usr.messages as msggroup by usr.id, usr.namehaving count(msg) = 0 JavaBean的屬性可以被綁定到一個命名查詢(named query)的參數(shù)上: 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(); 通過將接口Query與一個過濾器(filter)一起使用,集合(Collections)是可以分頁的: Query q = s.createFilter( collection, "" ); // 一個簡單的過濾器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.iterate("select count(*) from ....").next() ).intValue(); |