When defining a calculated property using a formula in NHibernate, what are the implications for when the formula varies its result depending on the query restrictions, especially with regards to query caching?
More specifically, consider the following simple C# class:
public class Entity
{
public Entity() { }
public virtual int Id { get; protected set; }
public virtual string Key { get; protected set; }
public virtual string Value { get; protected set; }
public virtual int Rank { get; protected set; }
}
Mapped with the following simple NHibernate mapping:
<class name="Entity" mutable="false">
<id name="Id">
<generator class="native">
</id>
<property name="Key"/>
<property name="Value"/>
<property name="Rank" formula="row_number() over(order by value)">
</class>
Running with a session factory with hibernate.cache.use_query_cache
option set to true
, and queried in the following ways:
ICriteria criteria = session.CreateCriteria(typeof(Entity));
criteria.SetCacheable(true);
criteria.SetCacheRegion("SearchResults");
IList<Entity> queryResult1 = criteria.List<Entity>();
criteria = session.CreateCriteria(typeof(Entity));
criteria.SetCacheable(true);
criteria.SetCacheRegion("SearchResults");
criteria.Add(Restrictions.Like("Key", "a", MatchMode.Anywhere));
IList<Entity> queryResult2 = criteria.List<Entity>();
Entity directResult = session.Load<Entity>(id);
Will NHibernate behave in a reasonable manner for the returned Entities? Or could the "Rank" value from one cached query pollute the Rank value of another query due to the query cache? Are there any other concerns when using such a formula in NHibernate mappings?
EDIT:
It might also be worth noting that in my particular case, "Entity" is not a first-class business entity, but sort of a meta-entity. It maps to an indexed database view over other first-class entities and is used exclusively for searching (the session.Load(id) call is contrived and should never actually happen in practice).
And, if there are implications to caching, as I suspect, what alternatives might exist for a similar use-case to avoid potential problems?