如何使nhibernate缓存通过多对一引用的表-我的配置正确吗? [英] How to get nhibernate to cache tables referenced via many-to-one - is my config correct?

查看:69
本文介绍了如何使nhibernate缓存通过多对一引用的表-我的配置正确吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力使它工作一段时间,但是没有运气.我想启用二级缓存,以防止从某些查找表中获取数据.

I've been trying to get this working for a while now with no luck. I want to enable the 2nd level cache to prevent fetching from some lookup tables.

我用代码设置了配置

cfg = new Configuration();
cfg.Properties[NHibernate.Cfg.Environment.ConnectionProvider] = "NHibernate.Connection.DriverConnectionProvider";
string connectionString = connection.ConnectionString;
cfg.Properties[NHibernate.Cfg.Environment.ConnectionString] = connectionString;
cfg.Properties[NHibernate.Cfg.Environment.ConnectionDriver] = "NHibernate.Driver.SqlClientDriver";
cfg.Properties[NHibernate.Cfg.Environment.Dialect] = "NHibernate.Dialect.MsSql2005Dialect";
cfg.Properties[NHibernate.Cfg.Environment.CommandTimeout] = "720";
cfg.Properties[NHibernate.Cfg.Environment.CacheProvider] = "NHibernate.Cache.HashtableCacheProvider";
cfg.Properties[NHibernate.Cfg.Environment.UseSecondLevelCache] = "true";
cfg.Properties[NHibernate.Cfg.Environment.UseQueryCache] = "true";
cfg.AddAssembly("APPName.PersistentEntities");
factory = cfg.BuildSessionFactory();

然后在我的xml配置中,添加了cache属性:

Then in my xml configuration I added the cache attribute:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class name="APPName.PersistentEntities.LockStatus, APPName.PersistentEntities" table="LockStatus" lazy="false">
    <meta attribute="class-description">lockstatus</meta>
    <cache usage="read-only" />
    <id name="Id" column="Id" type="Int32" unsaved-value="0">
      <generator class="native"></generator>
    </id>
    <property name="Name" column="Name" type="String" length="100" not-null="true"/>
  </class>
</hibernate-mapping>

然后从其他未缓存的实体中引用该实体

This entity is then referenced from other uncached entities

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
    <class name="APPName.PersistentEntities.Entity, APPName.PersistentEntities" table="Entity" lazy="false">
        <meta attribute="class-description">entity</meta>
        <id name="Id" column="Id" type="Int32" unsaved-value="0">
            <generator class="native">
            </generator>
        </id>

        <property name="Name" column="Name" type="String" length="500" not-null="true">
            <meta attribute="field-description">name</meta>
        </property>

        <many-to-one name="LockStatus" column="LockStatusId" class="APPName.PersistentEntities.LockStatus, APPName.PersistentEntities" not-null="false" lazy="false">
        </many-to-one>
    </class>
</hibernate-mapping>

>

我用类似这样的内容查询另一个实体:

I query this other entity with something like:

session = factory.OpenSession();
IList<T> s = session.CreateSQLQuery(query).AddEntity(typeof(T)).SetCacheable(true).List<T>();
session.Clear();
session.Close();

查询和映射运行良好,因此为了确保使用缓存,我尝试更新数据库中的名称.当我再次在应用程序中单击时,我看到了更新的名称,因此我认为它没有使用缓存重新查询.

The query and mappings run fine so to make sure its using the cache I try updating the names in the database. When I click again in the application I see the updated names so I assume it is not using the cache re-querying.

推荐答案

,您还需要在该关系上添加一个缓存声明(LockStatus).

you need to add a cache declaration on the relation as well (LockStatus).

还-

  1. 您可以使用 nHibernate的日志记录在每次调用中发送的sql.
  2. 我不明白您为什么要使用SQLQuery;您可以简单地使用QueryQueryOver.
  1. you can use nHibernate's logging to see exactly the sql sent on every call.
  2. I don't see why you'd want to use an SQLQuery in your case; you can simply use Query or QueryOver.

这篇关于如何使nhibernate缓存通过多对一引用的表-我的配置正确吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆