NHibernate的不级联删除儿童 [英] nhibernate does not cascade delete children

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

问题描述

的情况如下,

我有3个对象(我简化了名称)名为父母,父母的儿童及放大器;孩子的孩子的结果
父母的孩子是在父母的一套,和孩子的孩子是儿童一套。

I have 3 objects (i simplified the names) named Parent, parent's child & child's child
parent's child is a set in parent, and child's child is a set in child.

映射如下(相关部分)

<set name="parentset"
     table="pc-table"
     lazy="false"
     fetch="subselect"
     cascade="all-delete-orphan"
     inverse="true">
  <key column=FK_ID_PC" on-delete="cascade"/>
  <one-to-many class="parentchild,parentchild-ns"/>
</set>

父项的子

<set name="childset"
     table="cc-table"
     lazy="false"
     fetch="subselect"
     cascade="all-delete-orphan"
     inverse="true">
  <key column="FK_ID_CC" on-delete="cascade"/>
  <one-to-many class="childschild,childschild-ns"/>
</set>

我想要实现的是,当我删除父,会有一个级联删除所有槽的方式对孩子的孩子。但目前情况是这样的。

What i want to achieve is that when i delete the parent, there would be a cascade delete all the way trough to child's child. But what currently happens is this.

(这纯粹是映射测试目的)
获得一个父实体(正常工作)

(this is purely for mapping test purposes) getting a parent entity (works fine)

IQuery query = session.CreateQuery("from Parent where ID =" + ID);
IParent doc = query.UniqueResult<Parent>();

现在删除的部分。

session.Delete(doc);
transaction.Commit();



已经解决了级联的不能插入空值的错误和逆我希望这个现在想删除后一切与此代码,但只有父被删除。

After having solved the 'cannot insert null value' error with cascading and inverse i hopes this would now delete everything with this code, but only the parent is being deleted.

我错过了我的东西映射这很可能错过?在正确的方向上的任何提示是无任欢迎!

Did i miss something in my mapping which is likely to be missed? Any hint in the right direction is more than welcome!

圣迭戈,谢谢大家的给点答案。 (和解释)

Diego, thank you for the to the point answer. (and explanation)

我卸下去了上删除=级联,这是因为我喜欢的程度地控制在代码,而不是在数据库中。

I went with removing the on-delete="cascade", this because I like as much control as possible in code and not in the database.

下面发布的代码是(工作)的结果。

The code posted below is the (working) result.

<set name="parentset"     
     table="pc-table"     
     cascade="all-delete-orphan"     
     inverse="true"
     batch-size="5">     
  <key column=FK_ID_PC"/>     
  <one-to-many class="parentchild,parentchild-ns"/>     
</set>

父项的子

<set name="childset"            
     table="cc-table"     
     cascade="all-delete-orphan" 
     batch-size="5"  
     inverse="true">            
  <key column="FK_ID_CC">            
  <one-to-many class="childschild,childschild-ns"/>            
</set>

希望这有助于人们同样的问题。

Hope this helps people with the same problem!

推荐答案

通过设置上删除=级联上的按键,你让数据库处理级联。

By setting on-delete="cascade" on the keys, you are letting the DB handle the cascading.

您与NHibernate生成您的模式?

Are you generating your schema with NHibernate?

我只是转载的例子,它工作得很好有和无的属性,如果删除它,NHibernate的做级联。

I just reproduced your example and it worked fine with and without that attribute. When removing it, NHibernate does the cascading.

BTW,使用为lazy =false的取=再选择不是东西,你应该在默认情况下做的。如果您删除这些属性,离开上删除=级联和变更梯级保存更新,你只有两个查询检索和删除父。

BTW, using lazy="false" fetch="subselect" is not something that you should do by default. If you remove those attributes, leave on-delete="cascade" and change cascade to save-update, you'll only have two queries to retrieve and delete a Parent.

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

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