JPA异常: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: 1

异常信息:

org.springframework.orm.ObjectOptimisticLockingFailureException: 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


异常分析:

ID没有值

  • 使用的是hibernate的saveOrUpdate方法保存实例。saveOrUpdate方法要求ID为null时才执行SAVE,在其它情况下执行UPDATE。在保存实例的时候是新增,但你的ID不为null,所以使用的是UPDATE,但是数据库里没有主键相关的值,所以出现异常。
  • SpringBoot集成JPA使用save(S entity)方法时,如果需要进行更新操作,一定需要先获取实体的ID
    /**
   * Saves a given entity. 
     * Use the returned instance for further operations 
     * as the save operation might have changed the
   * entity instance completely.
   *
   * @param entity must not be {@literal null}.
   * @return the saved entity will never be {@literal null}.
   */
  <S extends T> S save(S entity);

 

ID有重复的

数据库ID重复

 

异常解决:

ID没有值

通过HQL更新和删除实体,一定需要先获取实体的ID


ID有重复的--解决

可参考:SQL:查找或删除重复行 https://blog.csdn.net/fly910905/article/details/79983334

查询及删除重复记录的SQL语句

 

1、查找表中多余的重复记录,重复记录是根据单个字段(Id)来判断

 

select * from 表 where Id in (select Id from 表 group byId having count(Id) > 1)

 

2、删除表中多余的重复记录,重复记录是根据单个字段(Id)来判断,只留有rowid最小的记录

 

DELETE from 表 WHERE (id) IN ( SELECT id FROM 表 GROUP BY id HAVING COUNT(id) > 1) AND ROWID NOT IN (SELECT MIN(ROWID) FROM 表 GROUP BY id HAVING COUNT(*) > 1);

 

3、查找表中多余的重复记录(多个字段)

 

select * from 表 a where (a.Id,a.seq) in(select Id,seq from 表 group by Id,seq having count(*) > 1)

 

4、删除表中多余的重复记录(多个字段),只留有rowid最小的记录

 

delete from 表 a where (a.Id,a.seq) in (select Id,seq from 表 group by Id,seq having count(*) > 1) and rowid not in (select min(rowid) from 表 group by Id,seq having count(*)>1)

 

5、查找表中多余的重复记录(多个字段),不包含rowid最小的记录

 

select * from 表 a where (a.Id,a.seq) in (select Id,seq from 表 group by Id,seq having count(*) > 1) and rowid not in (select min(rowid) from 表 group by Id,seq having count(*)>1)


相关文章
|
分布式计算 运维 数据挖掘
maxcomputer
maxcomputer
4313 2
|
分布式计算 Java Linux
Java 生成 UUID
Java 生成 UUID
11142 1
|
SQL Java 数据库连接
hibernate.exception.GenericJDBCException: could not extract ResultSet 解决办法
这句话翻译过来就是无法提取ResultSet 我在联查表的视图的时候发现的问题,明明之前好好的 那么你就得想想了 你再把错误信息往上翻翻,能不能看到 no viable alternative at input‘XXXX’ 是不是在关联表里更改了字段名视图里没改,字段名都不匹配它找个屁啊(笑)
|
SQL 数据库
SQL INSERT INTO SELECT 语句
SQL INSERT INTO SELECT 语句
2023 8
|
9月前
|
IDE 开发工具
【开发IDE升级】如何对IDEA版本进行升级
本文介绍了如何将 IntelliJ IDEA Ultimate 从 2020.2.2 版本升级到 2022.3.2 版本。主要内容包括准备工作、卸载旧版本和安装新版本的步骤。首先,从官网下载所需版本并备份旧版配置;接着,通过 Uninstall.exe 卸载旧版,保留配置和插件;最后,安装新版并完成激活。详细的操作步骤和截图帮助用户顺利完成升级过程。
10852 1
【开发IDE升级】如何对IDEA版本进行升级
|
XML Java Maven
nested exception is java.io.FileNotFoundException: class path resource [springmvc.xml] cannot be ope
nested exception is java.io.FileNotFoundException: class path resource [springmvc.xml] cannot be ope
546 0
nested exception is java.io.FileNotFoundException: class path resource [springmvc.xml] cannot be ope
|
11月前
|
存储 前端开发 Java
Java后端如何进行文件上传和下载 —— 本地版(文末配绝对能用的源码,超详细,超好用,一看就懂,博主在线解答) 文件如何预览和下载?(超简单教程)
本文详细介绍了在Java后端进行文件上传和下载的实现方法,包括文件上传保存到本地的完整流程、文件下载的代码实现,以及如何处理文件预览、下载大小限制和运行失败的问题,并提供了完整的代码示例。
4242 2
|
存储 Java 数据库连接
JPA 之 Hibernate EntityManager 使用指南
JPA 之 Hibernate EntityManager 使用指南
1381 0
|
存储 缓存 DataWorks
DataWorks操作报错合集之配置项目连通oss数据源 , 报The request signature we calculated does not match the signature you provided.如何解决
DataWorks是阿里云提供的一站式大数据开发与治理平台,支持数据集成、数据开发、数据服务、数据质量管理、数据安全管理等全流程数据处理。在使用DataWorks过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
|
监控 Java 网络安全
java获取ssh连接时报错com.jcraft.jsch.JSchException: Packet corrupt如何处理?
【6月更文挑战第5天】java获取ssh连接时报错com.jcraft.jsch.JSchException: Packet corrupt如何处理?
2313 5