【发布时间】:2014-05-20 12:42:31
【问题描述】:
我有一个类别的映射:
<class name="Category" table="Category" lazy="true">
<cache usage="read-write"/> // enabled Entity Cache
<id name="CategoryId" column="pk_cat_id">
<generator class="hilo"/>
</id>
<property name="Name" column="name" type="string" length="50" />
<many-to-one name="ParentCategory" class="Category"
column="parent_cat_id" cascade="save-update"/>
<bag name="childCategories" cascade="all" inverse="true">
<key column="parent_cat_id"/>
<one-to-many class="Category"/>
</bag>
</class>
我写道:
using (ISession sess = factory.OpenSession())
using (ITransaction tran = sess.BeginTransaction())
{
IList<Category> products = sess.CreateCriteria(typeof(Category))
.SetCacheable(true)
.List<Category>(); // hits DB -- no issues
IList<Category> prod = sess.CreateCriteria(typeof(Category))
.SetCacheable(true)
.Add(Restrictions.Eq("CategoryId", 4128768))
.List<Category>(); // hits DB -- WHY?
tran.Commit();
}
如您所见,对于第一个查询,它会获取表中的所有行。第二次,当我尝试访问由于第一次查询而已经存在的行时(CategoryId“= 4128768)。然后根据nHibernate,它应该在会话缓存中并且它不应该再次命中数据库。
我哪里错了?我是……
请帮帮我!!
【问题讨论】:
标签: c# nhibernate orm