NHibernate 命名查询和二级缓存

发布于 2024-08-09 17:06:08 字数 2662 浏览 6 评论 0原文

我在 hibernate.cfg.xml 中有以下映射

<?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>

和以下配置

<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(true) 运行命名查询时,它不会到达二级缓存。有什么理由吗?

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

谢谢!

I have the following mapping

<?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>

and the following configuration in 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>

Now when I run the named query with SetCacheable(true) which calls out to the mapped collection, it doesn't get to the 2nd level cache. Is there any reason why?

More Generically, is there a way to put the resultset of a named query into second level cache?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

美人如玉 2024-08-16 17:06:08

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

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

2. 在获取 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 文档二级缓存。

In order for queries to use the second level cache, two things must be done:
1. Enable the query cache in the NHibernate config:

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

2. Enable the query for caching when getting the IQuery instance:

    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();

These settings cause the results of the queries to be put in the second level cache. But the second level query cache stores only identifiers of entities, not the entities themselves. In order for the execution of the query to avoid the database completely, the entities must be cached as well. See the NH Docs for more explanation of the interaction of the second level caches.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文