无法删除集合:[NHibernate.Exceptions.GenericADOException] [英] could not delete collection : [NHibernate.Exceptions.GenericADOException]

查看:263
本文介绍了无法删除集合:[NHibernate.Exceptions.GenericADOException]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表,表A和tableB的



表A有列:tabAId,COL2,COL3 (tabAId的PrimaryKey和标识列)。



tableB的有柱:tabAId,名称(tabAId不为null)



我在表A的HBM文件中创建袋,以保持关系船

 <包名称=tableB的懒= 真正的逆=false的
批量大小=25级联=全删除,孤儿>
<键列=tabAId/>
将;一对许多类=tableB的/>
< /袋>

当我尝试更新记录的表A 扔例外,如我在表A的实例孩子的名单。




[NHibernate.Exceptions.GenericADOException] = {无法删除集合:[MIHR.Entities.tableA.tableB#21] [SQL :UPDATE dbo.tableB SET tabAId = NULL WHERE tabAId = @ P0]}



的InnerException = {无法插入NULL值插入列'tabAId',表'SA_MIHR_DEV.dbo.tableB';
列不允许空UPDATE fails.\r\\\
The语句已终止}



解决方案

有只有两种方式如何解决这个问题。



1)做不使用 逆=FALSE

 <包名称=tableB的懒=真正的逆=真//而不是虚假的
批量大小=25级联=全删除,孤儿>
<键列=tabAId/>
将;一对许多类=tableB的/>
< /袋>

这设置的(逆=真)的将指导NHibernate的直接删除DB中的项目



在使用逆=FALSE将一般总是导致:




  • UPDATE(用null)==从集合中移除

  • 的行为
  • 删除项目==级联行为



2)使参考列可为空



这意味着,我们可以离开NHibernate的做UPDATE和DELETE。因为列现在空。



这只有两种方法如何在这里解决。



我更倾向于将:< STRONG>逆=真



要使用的 逆=真 :我们总是要在C#中分配关系的两侧。这是添加)必须的(,INSERT操作:

 父父=新的父(); 
儿童的孩子=新的儿童
{
...
父父=,
};
//除非在父类初始化,我们可以在这里
parent.Children = parent.Children做到这一点?新的List<儿童>();
parent.Children.Add(小孩);

//现在只是家长可节省
//和NHibernate将尽一切级联预期
//而且由于逆映射 - 最有效的方法
session.Save(父);



我们可以看到,我们的已分配 - 明确 - 两个边关系。这是必须从NHibernate的逆映射获利。它也是很好的做法,因为后来,当我们从数据库加载数据我们预计,这NHibernate的会照顾有关设置,对于我们


I have Two Table , tableA and tableB.

tableA have column : tabAId, col2, col3 (tabAId primaryKey and Identity column.)

tableB have column : tabAId, name (tabAId is not null)

I have create Bag in hbm file of tableA, to maintain relation ship.

<bag name="tableB" lazy="true" inverse="false"
                    batch-size="25" cascade="all-delete-orphan">
  <key column="tabAId" />
  <one-to-many class="tableB" />
</bag>

When I try to update record in tableA it throw exception, where as I have list of child in tableA instance.

[NHibernate.Exceptions.GenericADOException] = {"could not delete collection: [MIHR.Entities.tableA.tableB#21][SQL: UPDATE dbo.tableB SET tabAId = null WHERE tabAId = @p0]"}

InnerException = {"Cannot insert the value NULL into column 'tabAId', table 'SA_MIHR_DEV.dbo.tableB'; column does not allow nulls. UPDATE fails.\r\nThe statement has been terminated."}

解决方案

There are only two ways how to solve this.

1) do not use inverse="false"

<bag name="tableB" lazy="true" inverse="true" // instead of false
                    batch-size="25" cascade="all-delete-orphan">
  <key column="tabAId" />
  <one-to-many class="tableB" />
</bag>

This setting (inverse="true") will instruct NHibernate to directly delete an item from DB.

While using inverse="false" will in general always lead to:

  • UPDATE (with null) == act of removing from collection
  • DELETE item == act of cascade

2) make the reference column nullable

That means, that we can leave NHibernate to do UPDATE and DELETE. Because column is now nullable.

These are only two ways how to solve it here.

My preference would be: inverse="true"

To work properly with inverse="true" we always have to assign both sides of relation in C#. This is a must for Add(), INSERT operation:

Parent parent = new Parent();
Child child = new Child
{
    ...
    Parent = parent,
};
// unless initialized in the Parent type, we can do it here
parent.Children = parent.Children ?? new List<Child>();
parent.Children.Add(child);

// now just parent could be saved
// and NHibernate will do all the cascade as expected
// and because of inverse mapping - the most effective way
session.Save(parent);

As we can see, we have assigned - explicitly - both sides of relationship. This is must to profit from NHibernate inverse mapping. And it is also good practice, because later, when we load data from DB we expect, that NHibernate will take care about setting that for us

这篇关于无法删除集合:[NHibernate.Exceptions.GenericADOException]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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