Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get nhibernate to cache tables referenced via many-to-one - is my config correct?
    primarykey
    data
    text
    <p>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.</p> <p>I set up my configuration in code</p> <pre><code>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(); </code></pre> <p>Then in my xml configuration I added the cache attribute:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; &lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"&gt; &lt;class name="APPName.PersistentEntities.LockStatus, APPName.PersistentEntities" table="LockStatus" lazy="false"&gt; &lt;meta attribute="class-description"&gt;lockstatus&lt;/meta&gt; &lt;cache usage="read-only" /&gt; &lt;id name="Id" column="Id" type="Int32" unsaved-value="0"&gt; &lt;generator class="native"&gt;&lt;/generator&gt; &lt;/id&gt; &lt;property name="Name" column="Name" type="String" length="100" not-null="true"/&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p>This entity is then referenced from other uncached entities</p> <pre><code>&lt;hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"&gt; &lt;class name="APPName.PersistentEntities.Entity, APPName.PersistentEntities" table="Entity" lazy="false"&gt; &lt;meta attribute="class-description"&gt;entity&lt;/meta&gt; &lt;id name="Id" column="Id" type="Int32" unsaved-value="0"&gt; &lt;generator class="native"&gt; &lt;/generator&gt; &lt;/id&gt; &lt;property name="Name" column="Name" type="String" length="500" not-null="true"&gt; &lt;meta attribute="field-description"&gt;name&lt;/meta&gt; &lt;/property&gt; &lt;many-to-one name="LockStatus" column="LockStatusId" class="APPName.PersistentEntities.LockStatus, APPName.PersistentEntities" not-null="false" lazy="false"&gt; &lt;/many-to-one&gt; &lt;/class&gt; &lt;/hibernate-mapping&gt; </code></pre> <p>></p> <p>I query this other entity with something like:</p> <pre><code>session = factory.OpenSession(); IList&lt;T&gt; s = session.CreateSQLQuery(query).AddEntity(typeof(T)).SetCacheable(true).List&lt;T&gt;(); session.Clear(); session.Close(); </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload