几天来我一直在与这个问题作斗争。
我有几个多对多关系的类,配置如下:
<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]
我一直在研究 inverse
和 cascade
值并在谷歌上搜索答案,但找不到任何解决方案。 inverse="true"
和 cascade="all/all-delete-orphan"
都没有成功。
这是与多对多关系相关的问题,还是我看错方向了?
我的类映射不正确吗?
编辑
这可能是 NHibernate 使用 DateTime 结构的方式导致的问题吗?
最佳答案
您不必将关联的一端标记为“inverse=true”,另一端标记为“inverse=false”吗? 通过这样做,您定义了协会的“所有者”。
关于c# - NHibernate:从多对多关系中删除 child 会导致异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3186896/