我正在使用Hibernate createcriteria来连接几个表并从数据库中查询数据。我发现结果返回与直接来自SQL客户端的结果查询与hibernate生成的SQL语句不同。 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 :(得分:0)
你不能做这样的比较,因为Hibernate有一级缓存(会话),如果你在这个缓存中修改了对象,它们将被调到结果中具有相同id的那些对象。这是Hibernate保证的对象上可重复读取的概念。
Direct SQL会绕过Hibernate的缓存,因此结果不会反映Hibernate会话中的任何本地更改。
如果您确实想要进行此比较,请确保您具有干净的会话,并且不会对您要查询的表进行其他更新。