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)


相关文章
|
Java
错误Batch update returned unexpected row count from update [0]; actual row count: 0;
错误Batch update returned unexpected row count from update [0]; actual row count: 0;   把开发过程中碰到的BUG累积下来也是一笔财富。
4194 0
错误Batch update returned unexpected row count from update [0]; actual row count: 0;
|
SQL Java 数据库连接
JPA 之 QueryDSL-JPA 使用指南
JPA 之 QueryDSL-JPA 使用指南
1529 0
|
Java 数据库连接 数据库
|
JSON Java 数据格式
解决json转换问题:net.sf.ezmorph.bean.MorphDynaBean cannotbe cast to xxx
解决json转换问题:net.sf.ezmorph.bean.MorphDynaBean cannotbe cast to xxx
9039 0
|
SQL 缓存 Java
如何在 Hibernate 中启用 SQL 日志记录?
【8月更文挑战第21天】
786 0
|
JavaScript Java 关系型数据库
Spring事务失效的8种场景
本文总结了使用 @Transactional 注解时事务可能失效的几种情况,包括数据库引擎不支持事务、类未被 Spring 管理、方法非 public、自身调用、未配置事务管理器、设置为不支持事务、异常未抛出及异常类型不匹配等。针对这些情况,文章提供了相应的解决建议,帮助开发者排查和解决事务不生效的问题。
2580 1
|
运维 监控 Java
SpringBootAdmin极简教程
SpringBoot应用可以通过Actuator来暴露应用运行过程中的各项指标,Spring Boot Admin通过这些指标来监控SpringBoot应用,然后通过图形化界面呈现出来。
992 0
SpringBootAdmin极简教程
[已解决]SpringDataJPA+Hibernate在执行executeUpdate()的时候报错 Executing an update/delete query
[已解决]SpringDataJPA+Hibernate在执行executeUpdate()的时候报错 Executing an update/delete query
|
XML SQL Java
玩转 Flowable 流程实例
玩转 Flowable 流程实例
1152 0