NHibernate命名查询和二级缓存

NHibernate命名查询和二级缓存,nhibernate,second-level-cache,named-query,Nhibernate,Second Level Cache,Named Query,我有以下映射 <?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 us

我有以下映射

<?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>
true
NHibernate.ByteCode.Castle.proxyFactory,NHibernate.ByteCode.Castle
真的
Y=真,N=假,0=假,1=真
真的
真的
Framework.Cache.SossCacheProvider,Framework.Cache.SossNHibernateCache
真的
现在,当我使用SetCacheable(true)运行调用映射集合的命名查询时,它不会到达二级缓存。有什么原因吗

更一般地说,有没有办法将命名查询的结果集放入二级缓存


谢谢

为了让查询使用二级缓存,必须做两件事:
1.在NHibernate配置中启用查询缓存:

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

这些设置会将查询结果放入二级缓存中。但是第二级查询缓存只存储实体的标识符,而不存储实体本身。为了使查询的执行完全避免数据库,还必须缓存实体。有关二级缓存交互作用的更多说明,请参阅。

我已经设置好了所有这些,NHProf告诉我,我没有使用二级缓存。唯一的区别是缓存的名称是在hbml.xml文件中定义的,而不是在代码中定义的。我认为需要更多的细节。行业类和NH配置文件的完整映射将是一个良好的开端。
    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();