c# - NHibernate:从多对多关系中删除 child 会导致异常

标签 c# nhibernate hibernate

几天来我一直在与这个问题作斗争。

我有几个多对多关系的类,配置如下:

<class name="Entry" table="Entries">
    <id name="Id" column="Id">
      <generator class="native" />
    </id>
    <timestamp name="LastUpdated" column="Entry_LastUpdated" generated="always" />
    ...
    <bag name="Sections" generic="true" table="EntrySections" inverse="false" cascade="none">
      <key column="EntrySection_EntryId" />
      <many-to-many column="EntrySection_SectionId" class="Blogs.BusinessLogic.Section, Blogs.BusinessLogic"/>
    </bag>
</class>

<class name="Section" table="Sections">
    <id name="Id" column="Id" unsaved-value="0">
      <generator class="native" />
    </id>
    <timestamp .../>
    ...
    <bag name="Entries" generic="true" table="EntrySections" inverse="false" cascade="none">
        <key column="EntrySection_SectionId" />
        <many-to-many column="EntrySection_EntryId" class="Blogs.BusinessLogic.Entry, Blogs.BusinessLogic"/>
    </bag>
</class>

它以我可以删除的方式配置,比方说,Section 对象及其与任何条目的关联都将被删除。

但是,当我只想删除关联时,例如:

entry.Sections.Remove(section);

这是我对该对象执行的唯一操作。但是,当 session 刷新时,会抛出以下 StaleObjectStateException 异常:

Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect): [Project.BusinessLogic.Entry#22]

我一直在研究 inversecascade 值并在谷歌上搜索答案,但找不到任何解决方案。 inverse="true"cascade="all/all-delete-orphan" 都没有成功。
这是与多对多关系相关的问题,还是我看错方向了?
我的类映射不正确吗?

编辑

这可能是 NHibernate 使用 DateTime 结构的方式导致的问题吗?

最佳答案

您不必将关联的一端标记为“inverse=true”,另一端标记为“inverse=false”吗? 通过这样做,您定义了协会的“所有者”。

关于c# - NHibernate:从多对多关系中删除 child 会导致异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3186896/

相关文章:

c# - 如何删除换行符以外的控制字符? (C#)

c# - Wix 使用编辑控件

nhibernate - NHibernate.MappingException : Could not compile the mapping document

c# - 有限制的 NHibernate QueryOver

java - 无法从实体类 Hibernate 4.2.7 Websphere 8.5.5 oracle 11g 创建表

java - Spring + hibernate : Inserting mutliple records and @Transactional

java - 多个线程访问数据库 : one with long transaction, 一个带有短事务

c# - 如何以编程方式将电子书发布到 Kindle、Sony Reader 等?

c# - WPF 值转换器 - 不可转换值的标准返回

NHibernate 延迟加载行为