Nhibernate - 项目将其 ParentID 更新为 null 而不是被删除

发布于 2024-12-20 17:48:28 字数 1115 浏览 0 评论 0原文

流程内的业务逻辑是:

  • 开始事务
  • 将项目添加到集合中
  • 执行 find("somethingA")
  • 根据上一步删除该项目。
  • 提交事务

我在我的父类中使用级联所有删除孤儿和 inverse=true 。从集合中删除项目时,我设置了 .parentObj = null 并从集合中删除了该项目。

使用 TemplateFlushMode.Auto 时,我对数据库进行了分析,并完成了以下操作:

  • INSERT item
  • SELECT related tosomethingA
  • UPDATE item 的parentID(父项的 FK)为 NULL

(插入项已完成,因为find() 已完成,以保证数据库中数据的一致性)。选择完成,然后我希望执行DELETE...但是对parentID = null 的更新已完成。

Find() 之前执行 Session.Flush() 时,会发生以下情况:

  • INSERT item
  • SELECT related tosomethingA
  • DELETE Item

使用 TemplateFlushMode.Commit 时code>,更改是在事务结束时完成的,并且永远不会插入项目:

  • SELECT related tosomethingA

我正在使用的应用程序正在使用 TemplateFlushMode.Auto,所以我想知道,是否有解释为什么如果在同一事务中添加一个项目然后删除一个项目之间进行选择,级联不起作用?

我知道出现的第一个答案是“不要将项目添加到集合中,如果它随后会被删除”。但我宁愿不改变业务逻辑。

有谁知道为什么使用AUTO时要进行更新而不是删除? (当使用 Commit 时,它不会插入对象)

The business logic inside a process is:

  • begin transaction
  • add an item to a collection
  • perform a find("somethingA")
  • delete that item depending on the previous step.
  • commit transaction

Im using cascade all-delete-orphans, and the inverse=true, both in my parent class. When removing the item from the collection, I set the .parentObj = null and remove the item from the collection.

When using TemplateFlushMode.Auto, I profiled the Database and the following is done:

  • INSERT item
  • SELECT related tosomethingA
  • UPDATE parentID (the FK to the parent) of the Item to NULL

(the insert item is done because a find() is done, to guarantee data consistency in the database). The the select is done, and then I would expect a DELETE to be performed... but an update to parentID = null is done.

When performing a Session.Flush() right before the Find(), the following happens:

  • INSERT item
  • SELECT related tosomethingA
  • DELETE Item

When using TemplateFlushMode.Commit, the changes are done at the end of the transaction and the item is never inserted:

  • SELECT related tosomethingA

The app I'm working with is using TemplateFlushMode.Auto, so I'm wondering, ,is there an explanation why the cascading is not working if a select is done in between one item is added and then removed in the same transaction?

I know the first answer that comes up is "don't add an item to a collection, if it will be removed afterwards". But I rather don't change the business logic.

Does anyone know why an update is being done instead of a delete when using AUTO? (when using Commit it does not insert the object)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我也只是我 2024-12-27 17:48:28

我想您应该在映射文件中使用 cascade="delete" 选项以便级联删除

I guess you should use the cascade="delete" option in your mapping file in order to cascade on delete

冷了相思 2024-12-27 17:48:28

您使用的是双向一对多绑定吗?您是否在 details 表中的外键上标记了 not-null="true" ?您能否发布您的映射 XML 文件以便更好地理解?

我知道我还没有回答你的问题,但我没有足够的声誉来发表评论。

Are you using a two way one to many binding ? Have you marked not-null="true" on the foreign key in the details table ? Can you please post your mappings XML file for better understanding ?

I know that i haven't answered your question, but I do not have enough reputation to post a comment instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文