抛出这个异常的原因并不惟一,所以此方法并不通用。在nhibernate中,默认认为,数据实体的增删改操作只影响数据库中的一行记录,如果ExecuteNonQuery操作返回的数字不为1,则会抛出此错误。比如我的一个项目中,无限级分类的增删改操作都由存储过程来实现,在表中有一列orders字段用作数据记录的排序用,增删改操作时,可能会有多行记录需要更改。我的解决办法是,在存储过程中需要更改的其它记录前设置 set nocount on,之后设置 set nocount off ,最后执行目的记录(此记录惟一)的增删改操作。补上一个示例存储过程:
CREATE   PROCEDURE CreateDepartment
(
 @parentId int=0,
 @Name nvarchar(30)='',
 @DepartmentID int out
)
as
set nocount on
declare @parentStr nvarchar(1000)
declare @maxOrders int
declare @rootId int
declare @depth int
declare @orders int
if @parentId=0
 begin
  set @parentStr=''
  set @Depth=0
  select @rootId=Max(rootId) FROM department WHERE ParentId=0
  if isnumeric(@rootid)=0 set @rootid=0
  if isnumeric(@maxorders)=0 set @maxorders=0
  set @RootId=@RootId+1 --添加1
  select @maxOrders=max(orders) from department where rootid<@rootid
 end
else
 begin
  select @parentStr=ParentStr,@RootId=RootId,@Depth=Depth FROM department WHERE DepartmentID=@parentId
  select @maxorders=max(Orders) from department where charindex(','+cast(@parentid as nvarchar)+',',','+parentstr+',')>0
  if isnumeric(@maxorders)=0 select @maxorders=orders from department where DepartmentID=@parentid
  set @parentStr=@parentStr+','
  if @parentStr='0,' set @parentStr=''
 end
set @parentstr=@parentstr+LTrim(cast(@parentId as varchar(10)))
set @depth=@depth+1
set @orders=@maxOrders+1
update department set childs=childs+1 where departmentid=@parentid
update department set Orders=orders+1 where orders>=@orders
set nocount off
insert department(Name,ParentId,ParentStr,RootId,childs,Depth,Orders)VALUES(@name,@ParentId,@ParentStr,@RootId,0,@Depth,@Orders)
set @DepartmentID=@@IDENTITY
GO

转载于:https://www.cnblogs.com/bqrm/archive/2008/02/24/1079890.html

Nhibernate中Unexpected row count的一种解决办法相关推荐

  1. 关于SharePoint V3网站老弹出“此网站需要运行以下载项:'Microsoft Corporation'中的'name.dll'......”的3种解决办法...

    有位朋友提出:SharePoint V3网站老弹出"此网站需要运行以下载项:'Microsoft Corporation'中的'name.dll'......"的骚扰信息. 我找了 ...

  2. Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1;

    Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1; 数据库表的i ...

  3. 解决Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1的一种方法

    使用JPA时,报错Batch update returned unexpected row count from update [0]; actual row count: 0; expected: ...

  4. org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actua

    hibernate更新数据的时候,如果数据不存在,就报org.hibernate.StaleStateException: Batch update returned unexpected row c ...

  5. JPA 异常Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1

    这个错误,看了网上大部分的贴子都说是主键自增设了值问题,但是我的主键非自增的手动赋值了,也出现此错误,原因如下: JPA使用 save更新数据的时候,如果数据没有任何变化也会报错,我们翻译一下这个错误 ...

  6. Json返回时间中出现乱码问题的两种解决方法

    Json返回时间中出现乱码问题的两种解决方法 参考文章: (1)Json返回时间中出现乱码问题的两种解决方法 (2)https://www.cnblogs.com/hanyinglong/archiv ...

  7. JPA 不在 persistence.xml 文件中配置每个Entity实体类的2种解决办法

    原文地址:http://www.cnblogs.com/taven/p/3351841.html JPA 不在 persistence.xml 文件中配置每个Entity实体类的2种解决办法 在Spr ...

  8. Java 的main方法中无法调用自身所在类的其他方法的两种解决办法

    前言 编程时我们会发现有时main方法中调用自身所在的类的其他方法时,发现无法调用,错误调用情况如下 错误情况代码 此处第3行调用test方法报错 public class Demo1{public ...

  9. win10突然只剩下c盘和d盘了_电脑C盘爆满飘红?系统卡?试试这两种解决办法

    编辑:晴晨 很多朋友在使用电脑时有一种习惯,就是将各种文件放到桌面,其中视频类.图纸类文件占用空间最多,比如下载个高清电影,随随便便都是几个G起步,好一点的片源可能要十几个G,所以再多的C盘空间也不够 ...

最新文章

  1. 单片机中的ROM,RAM和FLASH的作用
  2. New beginning
  3. 一个农场有头母牛,现在母牛才一岁,要到四岁才能生小牛,四岁之后,每年生一头小牛,n年后有多少头小牛
  4. 7.2 伪逆和线性方程 $A\mathbf{x}=\mathbf{b}$
  5. 什么是Handler(二)
  6. 下面哪项属于计算机在教育教学中的应用,东师现代教育技术18秋在线作业2答案...
  7. 微信公众号使用LocalStorage解决返回缓存问题
  8. C++的堆与拷贝构造函数
  9. 【概率笔记】条件概率这样学才快啦
  10. 【突发】解决remote: Support for password authentication was removed on August 13, 2021. Please use a perso
  11. Ubuntu 系列学习(四)ubuntu 22.04 安装 微信、QQ
  12. IROS 2021最新开源!深度学习三维激光雷达动态物体分割
  13. php汉字转换拼音,php中怎么将中文转换拼音
  14. oracle根据身份证号码计算年龄
  15. CSR8670学习笔记:OTA升级固件
  16. 模拟手机预览(非F12)
  17. VMware workstation的三种网络模式
  18. 唠唠SE的集合-01——Collection接口
  19. 收集一些新婚祝词(成语)
  20. 联想发布数据智能战略 未来五年赋能十万家企业

热门文章

  1. opencv-api houshlinesp
  2. sqlalchemy create engine
  3. Pandas 文本数据方法 findall( )
  4. tp5写的系统比php源码写的慢多少,基于TP5框架开发的极速企业网站开发框架PHP源码...
  5. 4. linux调用文件计算阶乘前5项和_嵌入式Linux系统编程——文件读写访问、属性、描述符、API
  6. VCHECK(每日报告)
  7. App后台开发运维和架构实践学习总结(4)——APP的注册和登录功能设计
  8. 大型网站技术架构(五)网站高可用架构
  9. oracle date 隐式转换,PL/SQL中的数据类型隐式转换规则
  10. 面试宝典系列-什么是心跳包机制