2015-05-22 29 views
2

我正在使用Hibernate createcriteria來連接幾個表並從數據庫中查詢數據。我發現結果返回不同於直接從SQL客戶端使用由hibernate生成的SQL語句的結果查詢。 由Hibernate返回的結果不僅僅是直接查詢。 我只是想知道爲什麼會發生?Hibernate返回結果是不同的直接查詢

下面是Hibernate個createCriteria生成的SQL語句。我用「?」取代了這個值佔位符在聲明中。

select this_.mrctBizId as mrctBizI1_5_13_, this_.name as 
name11_5_13_, 
from go.go_mrct_business this_ 
inner join go.go_location location3_ on this_.LocId=location3_.locId 
inner join go.go_state state4_ on location3_.StateId=state4_.stateId 
inner join go.go_country country5_ on state4_.CountryId=country5_.countryId 
left outer join go.go_mrct_off_date holiday7_ on this_.mrctBizId=holiday7_.MrctBizId and ((holiday7_.startDt<='2015-05-23' and holiday7_.endDt>='2015-05-23')) 
left outer join go.go_mrct_business gomrctbusi13_ on holiday7_.MrctBizId=gomrctbusi13_.mrctBizId 
left outer join go.go_mrct_off_day offday6_ on this_.mrctBizId=offday6_.MrctBizId and (offday6_.offDay=7) 
left outer join go.go_mrct_business gomrctbusi15_ on offday6_.MrctBizId=gomrctbusi15_.mrctBizId 
inner join go.go_service service1_ on this_.mrctBizId=service1_.MrctBizId 
inner join go.go_category gocategory2_ on service1_.CatId=gocategory2_.catId 
left outer join go.go_mrct_business gomrctbusi20_ on service1_.MrctBizId=gomrctbusi20_.mrctBizId  
where offday6_.MrctBizId is null 
and holiday7_.MrctBizId is null 
and this_.enabled=true 
and service1_.enabled=true 
and gocategory2_.catId=11 
and country5_.countryId=1 
and state4_.stateId=2 
and location3_.locId=6 
and this_.bizStartTime<=CAST('10:10:00' AS time) 
and this_.bizEndTime>=CAST('10:10:00' AS time) ; 
+0

請添加更多細節,如「直接」sql查詢和條件查詢。 –

+0

上面的SQL語句是由hibernate createCriteria生成的查詢,而且我也使用這個SQL語句直接通過SQL客戶端查詢,但都返回不同的結果 –

回答

0

你不能做這樣的比較,因爲Hibernate有一個一級緩存(會話),如果你已經在這個緩存修改的對象,他們將被攪和到結果具有相同ID的對象。這是Hibernate保證的對象可重複讀取的概念。

直接SQL繞過Hibernate的緩存,這樣的結果將不會反映在Hibernate會話的修改。

如果你真的想做這個比較,請確保你有一個乾淨的會話,並且沒有其他更新發生在你正在查詢的表上。

+0

即使我通過JUnit測試它測試嗎?我試着用JUnit,結果仍然一樣。 –

+1

是的,語義不會改變它被調用的方式。您可以確保會話清晰,並且這是在會話中運行的唯一查詢。但我仍然不會推薦做這樣的比較。 – codedabbler

相關問題