二级缓存和查询缓存的结合使用


 
 
     String hsql=" from Organization  porg left join fetch porg.childern as corg ";
        List<Organization> results=super.getSession().createQuery(hsql).setCacheable(true).list();
        当没有配置二级缓存,只配置查询缓存是
        第一次的sql为
     
         select
                organizati0_.ID as ID0_0_,
                childern1_.ID as ID0_1_,
                organizati0_.ORGANIZATION_NAME as ORGANIZA2_0_0_,
                organizati0_.ORGANIZATION_SN as ORGANIZA3_0_0_,
                organizati0_.DESCRIPTION as DESCRIPT4_0_0_,
                organizati0_.PID as PID0_0_,
                organizati0_.ORGANIZATION_TEL as ORGANIZA6_0_0_,
                organizati0_.ORDER_NUM as ORDER7_0_0_,
                organizati0_.PERSON_ID as PERSON8_0_0_,
                childern1_.ORGANIZATION_NAME as ORGANIZA2_0_1_,
                childern1_.ORGANIZATION_SN as ORGANIZA3_0_1_,
                childern1_.DESCRIPTION as DESCRIPT4_0_1_,
                childern1_.PID as PID0_1_,
                childern1_.ORGANIZATION_TEL as ORGANIZA6_0_1_,
                childern1_.ORDER_NUM as ORDER7_0_1_,
                childern1_.PERSON_ID as PERSON8_0_1_,
                childern1_.PID as PID0__,
                childern1_.ID as ID0__    
        from
                g_organization organizati0_    
        left outer join
                g_organization childern1_    
                        on organizati0_.ID=childern1_.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=?
        
     
            
            但当配置上二级缓存,不配置查询缓存的话。则第二次点击仍然会发出查询语句
          
        select
                     organizati0_.ID as ID0_0_,
                     childern1_.ID as ID0_1_,
                     organizati0_.ORGANIZATION_NAME as ORGANIZA2_0_0_,
                     organizati0_.ORGANIZATION_SN as ORGANIZA3_0_0_,
                     organizati0_.DESCRIPTION as DESCRIPT4_0_0_,
                     organizati0_.PID as PID0_0_,
                     organizati0_.ORGANIZATION_TEL as ORGANIZA6_0_0_,
                     organizati0_.ORDER_NUM as ORDER7_0_0_,
                     organizati0_.PERSON_ID as PERSON8_0_0_,
                     childern1_.ORGANIZATION_NAME as ORGANIZA2_0_1_,
                     childern1_.ORGANIZATION_SN as ORGANIZA3_0_1_,
                     childern1_.DESCRIPTION as DESCRIPT4_0_1_,
                     childern1_.PID as PID0_1_,
                     childern1_.ORGANIZATION_TEL as ORGANIZA6_0_1_,
                     childern1_.ORDER_NUM as ORDER7_0_1_,
                     childern1_.PERSON_ID as PERSON8_0_1_,
                     childern1_.PID as PID0__,
                     childern1_.ID as ID0__    
             from
                     g_organization organizati0_    
             left outer join
                     g_organization childern1_    
                             on organizati0_.ID=childern1_.PID
                            

        但当配置了二级缓存,再配上查询缓存时。
         第一次为
         
    select
                organizati0_.ID as ID0_0_,
                childern1_.ID as ID0_1_,
                organizati0_.ORGANIZATION_NAME as ORGANIZA2_0_0_,
                organizati0_.ORGANIZATION_SN as ORGANIZA3_0_0_,
                organizati0_.DESCRIPTION as DESCRIPT4_0_0_,
                organizati0_.PID as PID0_0_,
                organizati0_.ORGANIZATION_TEL as ORGANIZA6_0_0_,
                organizati0_.ORDER_NUM as ORDER7_0_0_,
                organizati0_.PERSON_ID as PERSON8_0_0_,
                childern1_.ORGANIZATION_NAME as ORGANIZA2_0_1_,
                childern1_.ORGANIZATION_SN as ORGANIZA3_0_1_,
                childern1_.DESCRIPTION as DESCRIPT4_0_1_,
                childern1_.PID as PID0_1_,
                childern1_.ORGANIZATION_TEL as ORGANIZA6_0_1_,
                childern1_.ORDER_NUM as ORDER7_0_1_,
                childern1_.PERSON_ID as PERSON8_0_1_,
                childern1_.PID as PID0__,
                childern1_.ID as ID0__    
        from
                g_organization organizati0_    
        left outer join
                g_organization childern1_    
                        on organizati0_.ID=childern1_.PID
                        

            而第二次 则直接从内存中获取了。
                       
            
                        
           

你可能感兴趣的:(java,sql,查询缓存,二级缓存,休闲)