强制 NHibernate 在插入之前级联删除 [英] Forcing NHibernate to cascade delete before inserts

查看:21
本文介绍了强制 NHibernate 在插入之前级联删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个父对象,它与子对象的 ISet 有一对多的关系.子对象具有唯一约束(PageNumContentID - 父对象的外键).

I have a parent object which has a one-to-many relationship with an ISet of child objects. The child objects have a unique constraint (PageNum and ContentID - the foreign key to the parent).

<set name="Pages" inverse="true" cascade="all-delete-orphan" access="field.camelcase-underscore">
    <key column="ContentId" />
    <one-to-many class="DeveloperFusion.Domain.Entities.ContentPage, DeveloperFusion.Domain, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</set>

我遇到的问题是,如果我从父集合中删除一个 ContentPage 元素,然后在同一个事务中添加一个具有相同唯一键的新元素......你会得到一个唯一的违反约束,因为NHibernate 尝试在删除之前 执行插入.

The problem I've hit is if I remove a ContentPage element from the parent collection, and then add a new one with the same unique key within the same transaction... You get a unique constraint violation because NHibernate attempts to perform the insert before the delete.

有没有办法强制 NHibernate 先执行删除操作?

Is there a way to force NHibernate to perform the delete first?

推荐答案

没有选项可以指定事务中的操作顺序,因为它是硬编码的,如下(来自文档):

There is no option to specify the order of operations in a transaction as it is hard-coded as follows (from the documentation):

SQL 语句按以下顺序发出

The SQL statements are issued in the following order

  • 所有实体插入,以相同的顺序使用 ISession.Save() 保存相应的对象
  • 所有实体更新
  • 所有集合删除
  • 所有集合元素的删除、更新和插入
  • 所有集合插入
  • 所有实体删除,以相同的顺序使用 ISession.Delete() 删除相应的对象

(一个例外是使用原生 ID 生成的对象在保存时被插入.)

(An exception is that objects using native ID generation are inserted when they are saved.)

因此,我能否请您回答为什么要添加具有现有标识符的新实体?标识符对于特定的实体"应该是唯一的.如果那个实体消失了,它的标识符也应该消失.

As such, can I challenge you to answer why you are adding a new entity with an existing identifier? An identifier is supposed to be unique to a specific "entity." If that entity is gone, so should be its identifier.

另一种选择是对该记录进行更新,而不是删除/插入.这使 ID 保持不变,因此不会违反唯一约束(至少在键上),您可以更改所有其他数据,使其成为新"数据.记录.

Another option would be to do an update on that record instead of a delete/insert. This keeps the ID the same so there is no unique constraint violation (on the key at least) and you can change all the other data so that it's a "new" record.

很明显,我在回答时并没有完全注意这个问题,因为这是一个对非主键列有唯一约束的问题.

So apparently I wasn't entirely paying attention to the question when I responded since this is a problem with a unique constraint on a non-primary-key column.

我认为您有两种解决方案可供选择:

I think you have two solutions to choose from:

  1. 在删除之后调用 Session.Flush() 这将执行到该点的会话的所有更改,之后您可以继续其余的操作(插入新对象).这也适用于事务内部,因此您无需担心原子性.
  2. 创建一个 ReplacePage 函数,用新数据更新现有实体,但保持主键和唯一列相同.
  1. Call Session.Flush() after your delete which will execute all the changes to the session up to that point, after which you can continue with the rest (inserting your new object). This works inside of a transaction as well so you don't need to worry about atomicity.
  2. Create a ReplacePage function which updates the existing entity with new data but keeps the primary-key and the unique column the same.

这篇关于强制 NHibernate 在插入之前级联删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆