1.查询缓存
在使用外连接进行查询时需要注意查询缓存的使用
hibernate的xml配置文件,当一对多中配置抓取策略为join时
<set inverse="true" access="field" name="childern" fetch="join" cascade="save-update,persist" >
<key column="PID"/>
<one-to-many class="com.going.platform.model.Organization"/>
</set>
String hsql=" from Organization porg left outer join fetch porg.childern as corg ";
List<Organization> results=super.getSession().createQuery(hsql).setCacheable(true).list();
进行查询时 第一次查询会使用外连接的方式来进行查询,查询的sql语句
select
this_.ID as ID0_1_,
this_.ORGANIZATION_NAME as ORGANIZA2_0_1_,
this_.ORGANIZATION_SN as ORGANIZA3_0_1_,
this_.DESCRIPTION as DESCRIPT4_0_1_,
this_.PID as PID0_1_,
this_.ORGANIZATION_TEL as ORGANIZA6_0_1_,
this_.ORDER_NUM as ORDER7_0_1_,
this_.PERSON_ID as PERSON8_0_1_,
childern2_.PID as PID3_,
childern2_.ID as ID3_,
childern2_.ID as ID0_0_,
childern2_.ORGANIZATION_NAME as ORGANIZA2_0_0_,
childern2_.ORGANIZATION_SN as ORGANIZA3_0_0_,
childern2_.DESCRIPTION as DESCRIPT4_0_0_,
childern2_.PID as PID0_0_,
childern2_.ORGANIZATION_TEL as ORGANIZA6_0_0_,
childern2_.ORDER_NUM as ORDER7_0_0_,
childern2_.PERSON_ID as PERSON8_0_0_
from
g_organization this_
left outer join
g_organization childern2_
on this_.ID=childern2_.PID
但配置了查询缓存 第二次点击时 则会产生如下sql语句
select
organizati0_.ID as ID0_1_,
organizati0_.ORGANIZATION_NAME as ORGANIZA2_0_1_,
organizati0_.ORGANIZATION_SN as ORGANIZA3_0_1_,
organizati0_.DESCRIPTION as DESCRIPT4_0_1_,
organizati0_.PID as PID0_1_,
organizati0_.ORGANIZATION_TEL as ORGANIZA6_0_1_,
organizati0_.ORDER_NUM as ORDER7_0_1_,
organizati0_.PERSON_ID as PERSON8_0_1_,
childern1_.PID as PID3_,
childern1_.ID as ID3_,
childern1_.ID as ID0_0_,
childern1_.ORGANIZATION_NAME as ORGANIZA2_0_0_,
childern1_.ORGANIZATION_SN as ORGANIZA3_0_0_,
childern1_.DESCRIPTION as DESCRIPT4_0_0_,
childern1_.PID as PID0_0_,
childern1_.ORGANIZATION_TEL as ORGANIZA6_0_0_,
childern1_.ORDER_NUM as ORDER7_0_0_,
childern1_.PERSON_ID as PERSON8_0_0_
from
g_organization organizati0_
left outer join
g_organization childern1_
on organizati0_.ID=childern1_.PID
where
organizati0_.ID=?
Hibernate:
select
organizati0_.ID as ID0_1_,
organizati0_.ORGANIZATION_NAME as ORGANIZA2_0_1_,
organizati0_.ORGANIZATION_SN as ORGANIZA3_0_1_,
organizati0_.DESCRIPTION as DESCRIPT4_0_1_,
organizati0_.PID as PID0_1_,
organizati0_.ORGANIZATION_TEL as ORGANIZA6_0_1_,
organizati0_.ORDER_NUM as ORDER7_0_1_,
organizati0_.PERSON_ID as PERSON8_0_1_,
childern1_.PID as PID3_,
childern1_.ID as ID3_,
childern1_.ID as ID0_0_,
childern1_.ORGANIZATION_NAME as ORGANIZA2_0_0_,
childern1_.ORGANIZATION_SN as ORGANIZA3_0_0_,
childern1_.DESCRIPTION as DESCRIPT4_0_0_,
childern1_.PID as PID0_0_,
childern1_.ORGANIZATION_TEL as ORGANIZA6_0_0_,
childern1_.ORDER_NUM as ORDER7_0_0_,
childern1_.PERSON_ID as PERSON8_0_0_
from
g_organization organizati0_
left outer join
g_organization childern1_
on organizati0_.ID=childern1_.PID
where
organizati0_.ID=?
个人认为是,hibernate中会得出拥有子节点的数据id、,然后根据Id进行外连接进行查询
但如果取消查询缓存的话 每次都会调用同一语句
select
this_.ID as ID0_1_,
this_.ORGANIZATION_NAME as ORGANIZA2_0_1_,
this_.ORGANIZATION_SN as ORGANIZA3_0_1_,
this_.DESCRIPTION as DESCRIPT4_0_1_,
this_.PID as PID0_1_,
this_.ORGANIZATION_TEL as ORGANIZA6_0_1_,
this_.ORDER_NUM as ORDER7_0_1_,
this_.PERSON_ID as PERSON8_0_1_,
childern2_.PID as PID3_,
childern2_.ID as ID3_,
childern2_.ID as ID0_0_,
childern2_.ORGANIZATION_NAME as ORGANIZA2_0_0_,
childern2_.ORGANIZATION_SN as ORGANIZA3_0_0_,
childern2_.DESCRIPTION as DESCRIPT4_0_0_,
childern2_.PID as PID0_0_,
childern2_.ORGANIZATION_TEL as ORGANIZA6_0_0_,
childern2_.ORDER_NUM as ORDER7_0_0_,
childern2_.PERSON_ID as PERSON8_0_0_
from
g_organization this_
left outer join
g_organization childern2_
on this_.ID=childern2_.PID
来进行查询。。。。
得出结论就是在使用外连接时,应该注意到并不是所有条件不会变化的sql语句,使用查询缓存的命中率就会高