2

我正在使用 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>
...

谢谢!

4

1 回答 1

0

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

例子:

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

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

于 2012-08-15T13:39:58.897 回答