如何在 NHibernate 中删除子对象? [英] How to delete child object in NHibernate?

查看:22
本文介绍了如何在 NHibernate 中删除子对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个父对象,它与子对象的 IList 有一对多的关系.删除子对象的最佳方法是什么?我没有删除父级.我的父对象包含子对象的 IList.这是一对多关系的映射:

I have a parent object which has a one to many relationship with an IList of child objects. What is the best way to delete the child objects? I am not deleting the parent. My parent object contains an IList of child objects. Here is the mapping for the one to many relationship:

<bag name="Tiers" cascade="all">
  <key column="mismatch_id_no" />
  <one-to-many class="TGR_BL.PromoTier,TGR_BL"/>
</bag>

如果我尝试使用 clear() 从集合中删除所有对象,然后调用 SaveOrUpdate(),我会收到此异常:

If I try to remove all objects from the collection using clear(), then call SaveOrUpdate(), I get this exception:

System.Data.SqlClient.SqlException: Cannot insert the value NULL into column

如果我尝试单独删除子对象,然后将它们从父对象中删除,则会出现异常:

If I try to delete the child objects individually then remove them from the parent, I get an exception:

deleted object would be re-saved by cascade

这是我第一次在 NHibernate 中处理删除子对象的问题.我做错了什么?

This is my first time dealing with deleting child objects in NHibernate. What am I doing wrong?

只是为了澄清 - 我不是要删除父对象,只是删除子对象.我将这种关系设置为父级一对多.我还需要在子对象映射上创建多对一关系吗?

edit: Just to clarify - I'm NOT trying to delete the parent object, just the child objects. I have the relationship set up as a one to many on the parent. Do I also need to create a many-to-one relationship on the child object mapping?

推荐答案

您会遇到第一个错误,因为当您从集合中删除项目时,NHibernate 的默认操作模式是简单地中断关联.在数据库中,NHibernate 尝试将子行上的外键列设置为空.由于该列中不允许为空,因此 SQL Server 会引发错误.清除集合不一定会删除子对象,但一种方法是设置cascade=all-delete-orphan.这会通知 NHibernate 它应该删除新孤立的行而不是设置外键列.

You are getting the first error because, when you remove the items from the collection, NHibernate's default mode of operation is to simply break the association. In the database, NHibernate tries to set the foreign key column on the child row to null. Since you do not allow nulls in that column, SQL Server raises the error. Clearing the collection will not necessarily delete the child object, but one way to do so is to set cascade=all-delete-orphan. This informs NHibernate that it should delete the newly orphaned rows instead of setting the foreign key column.

您收到第二个错误,因为当您调用 SaveOrUpdate NHibernate 首先删除所有子对象时.然后,因为两个关系都没有被标记为反向,NHibernate 还尝试将子表中的外键列设置为空.由于行已被删除,您会收到第二个错误.您需要在关系的一侧设置 inverse=true 来解决此问题.这通常在一侧(主键或父键)完成.如果您不这样做,NHibernate 将为关系的每一方进行适当的更新.不幸的是,运行两次更新并不合适.

You are getting the second error because when you call SaveOrUpdate NHibernate first deletes all of the child objects. Then, because neither relationship is marked as inverse, NHibernate also tries to set the foreign key column in your child table to null. Since the rows have already been deleted, you receive the second error. You need to set inverse=true on one side of your relationship to fix this. This is usually done on the one (primary key or parent) side. If you do not do this, NHibernate will make the appropriate updates for each side of the relationship. Unfortunately, running two updates is not the appropriate thing to do.

您应该始终将关系的一侧标记为相反的一侧.根据您的编码方式,您可能需要也可能不需要使用级联.如果您想在尝试使用 Clear() 时利用一次性删除,则需要定义级联.

You should always mark one side of your relationships as the inverse side. Depending on how you code, you may or may not need to use cascading. If you want to take advantage of one shot deletes as you are trying to do using Clear(), you need to define your cascade.

这篇关于如何在 NHibernate 中删除子对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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