异常信息为:org.springframework.orm.hibernate5.HibernateOptimisticLockingFailureException
![搜索](https://i-blog.csdnimg.cn/blog_migrate/a70bb6fd41d0de0d8cf45a69f0247ec3.png)
: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1; nested exception is org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
出现这个异常的原因有三个:1、这个异常是由于主键设置为自增长,而在我们插入记录的时候设置了ID的值导致的。
2、使用的是hibernate的saveOrUpdate方法保存实例
saveOrUpdate要求:只有ID为null才执行save,其他情况下执行update
在保存实例的时候是新增,但你的ID不为null,所有就执行update操作,可是数据库没有主键对应的值,所有出现了异常。
今天出现的result error 导致的异常,是因为saveOrUpdate冲突
3、在Hibernate映射一对多,多对一,多对多的时候新增常常会出现异常