NHibernate - 二级集合缓存不一致

时间:2012-08-10 14:30:39

标签: c# nhibernate caching

我正在使用NHibernate 3.2的二级缓存(SysCache)。我正在删除该集合并在同一事务中写入新元素。但是,这会破坏缓存,因为它仍然保留对已删除元素的引用。交易代码:

 RepositorioMovimentacao.Delete(notaHibernate.MovimentacaoItemEquipList);
 RepositorioNota.Update(notaHibernate);
 RepositorioMovimentacao.Save(model.MovimentacaoItemEquipList);
 Session.Commit()

在其他会话上检索实体时抛出异常:

No row with the given identifier ...

这是我的映射:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="CMD.Business.BusinessEntity" namespace="CMD.Business.BusinessEntity.Entities">
  <class name="NotaMovimentacaoEquipamento" table="CEM.NOTA_MOVIMENTACAO_EQUIPAMENTO" lazy="true" dynamic-update="true" dynamic-insert="true" >
    <cache  usage="read-write"/>
...
    <bag name="MovimentacaoItemEquipList" inverse="true" generic="true" lazy="true" >
      <cache  usage="read-write"/>
      <key>
        <column name="NTMO_SQ_NOTA_MOVIMENTACAO"/>
      </key>
      <one-to-many class="MovimentacaoItemEquip" />
    </bag>
...

多对一映射:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="CMD.Business.BusinessEntity" namespace="CMD.Business.BusinessEntity.Entities">
    <class name="MovimentacaoItemEquip" table="CEM.MOVIMENTACAO_ITEM_EQUIP" lazy="true" dynamic-update="true" dynamic-insert="true" >
    <cache  usage="read-write"/>
...
  <many-to-one name="NotaMovimentacaoEquipamento" class="NotaMovimentacaoEquipamento">
      <column name="NTMO_SQ_NOTA_MOVIMENTACAO" />
    </many-to-one>
...

谢谢!

1 个答案:

答案 0 :(得分:0)

找到解决方案。删除父集合时,请始终在提交之前将父集合设置为null。

示例:

ChildRepository.Delete(parent.ChildList)
parent.ChildList = null;

不这样做会导致缓存损坏。