HQL學習小結

1 join
a 爲相關聯的實體或集合指定一個別名
select mate
from Cat as cat
inner join cat.mate as mate
==
select cat.mate from Cat cat
b 外連接只能由於在映射中有配置關聯表的類
select mate
from Cat as cat
left join cat.mate as mate
有一點要注意,使用外連接select不能省,否則將會返回一個對象數組。

2 fetch
要和join一起使用
fetch可以使延遲加載的屬性立即加載
from TestUser t join fetch t.testEnterprise where t.id = 4
fetch不要和iterate(),setMaxResults(),setFirstResult()一起使用
因爲使用了fecth後真實查詢的結果集不僅僅只是from後面的表的字段

3 select的數據組織
a 存放在 Object[]隊列
select mother, offspr, mate.name
from DomesticCat as mother
inner join mother.mate as mate
left outer join mother.kittens as offspr
b 存放在一個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
c 存放在一個實際的類型安全的Java對象
select new Family(mother, mate, offspr)
from DomesticCat as mother
join mother.mate as mate
left join mother.kittens as offspr
d 存放在一個Map對象
select new map( max(bodyWeight) as max, min(bodyWeight) as min, count(*) as n )
from Cat cat

4 隱藏屬性
一個“任意”類型有兩個特殊的屬性id和class
from AuditLog log, Payment payment
where log.item.class = 'Payment' and log.item.id = payment.id

5 多態查詢
from java.lang.Object o

from domain.AbstractTestUser
這裏注意除了映射過的表實體類型,其他的都要使用類全名

6 特殊的函數
HQL index() 函數,作用於join的有序集合的別名。
HQL函數,把集合作爲參數:size(), minelement(), maxelement(), minindex(), maxindex(),還有特別的elements() 和indices函數,可以與

數量詞加以限定:some, all, exists, any, in。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章