2009-11-03 98 views
0

我有以下映射NHibernate的命名查詢和二級緩存

<?xml version="1.0" encoding="utf-8" ?> 
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" 
       assembly="Domain.Core" 
       namespace="Domain.Core"> 
<class name="Industry" table="INDUSTRY"> 
<cache usage="read-write" region="IndustryCache" include="all"/> 
<id name="Id" type="Int64" column="IID"> 
    <generator class="sequence"> 
    <param name="sequence">INDUSTRY_SEQ</param> 
    </generator> 
</id> 
<version name="Version" column="VERSION" access="property" unsaved-value="null" generated="never"/> 
<property name="CreationTime" column="CREATE_DATE" type="DateTime" not-null="true" /> 
<property name="CreatedBy" column="CREATE_USER" type="String" not-null="true" /> 
<property name="LastUpdateTime" column="MODIFY_DATE" type="DateTime" not-null="false" /> 
<property name="LastUpdateBy" column="MODIFY_USER" type="String" not-null="false" /> 
<property name="Code" column="INDUSTRY" type="String" not-null="false" /> 
<map name="Resources" table="INDUSTRY_TL" fetch="subselect"> 
    <cache region="IndustryCache" usage="read-write" include="all"/> 
    <key column="INDUSTRY_ID"/> 
    <composite-index class="Framework.Globalization.UILanguage, Framework"> 
    <key-property name="Code" column="LANG" access="property" /> 
    </composite-index> 
    <composite-element class="Industry+Translation"> 
    <property name="Name" column="Industry_TL" /> 
    </composite-element> 
</map> 
</class> 
<query name="GetIndustyOrderByName"> 
<![CDATA[ 
from Industry as i left join fetch i.Resources as res where INDEX(res) = :lang order 
by res.Name 
]]> 
</query>  
</hibernate-mapping> 

,並在hibernate.cfg.xml以下配置

<property name="show_sql">true</property> 
<property name='proxyfactory.factory_class'>NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle</property> 
<property name='prepare_sql'>true</property> 
<property name='query.substitutions'>Y=true,N=false,0=false,1=true</property> 
<property name="generate_statistics">true</property> 
<property name="cache.use_second_level_cache">true</property> 
<property name="cache.provider_class">Framework.Cache.SossCacheProvider, Framework.Cache.SossNHibernateCache</property> 
<property name="cache.use_query_cache">true</property> 

現在,當我運行SetCacheable命名查詢(真)調用映射集合,它不會到達二級緩存。有什麼理由?

更多通常,有沒有辦法將命名查詢的結果集放入二級緩存中?

謝謝!

回答

2

爲了查詢使用二級緩存,兩件事情必須做:起牀時

<property name="cache.use_query_cache">true</property> 

2.啓用緩存查詢:
1.啓用NHibernate的配置查詢緩存在IQuery實例:

IQuery = session.GetNamedQuery("from Industry as i left join fetch i.Resources as res where INDEX(res) = :lang order by res.Name") 
     .SetCacheable(true) 
     .Setxxx(); 

這些設置會導致查詢的結果放在二級緩存。但是第二級查詢緩存僅存儲實體的標識符,而不是實體本身。爲了執行查詢以完全避免數據庫,實體也必須被緩存。有關二級緩存交互的更多說明,請參見NH Docs

+0

我確實已經設置好了,NHProf告訴我我沒有達到二級緩存。唯一的區別是,緩存的名稱是在hbml.xml文件中定義的,而不是在代碼中定義的。 – Herman 2009-11-04 03:39:15

+0

我認爲更多細節是必要的。行業類和NH配置文件的完整映射將是一個好的開始。 – 2009-11-04 12:41:39

+0

添加到組織中的更多信息。題 – Herman 2009-11-04 14:45:14