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

查看:160
本文介绍了如何在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:只是为了澄清-我不是要删除父对象,而是子对象.我在父母之间建立了一对多的关系.我还需要在子对象映射上创建多对一关系吗?

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尝试将子行上的外键列设置为null.由于不允许在该列中使用null,因此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还将尝试将子表中的外键列设置为null.由于行已被删除,您将收到第二个错误.您需要在关系的一侧设置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天全站免登陆