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)


目录
相关文章
|
7月前
|
druid Java
Error attempting to get column ‘createTime‘ from result set的异常
Error attempting to get column ‘createTime‘ from result set的异常
448 0
|
数据库 OceanBase
LIMIT_ROW_COUNT
LIMIT_ROW_COUNT
101 1
|
程序员 Go API
译|Errors are values
译|Errors are values
77 0
|
Java 数据库连接 mybatis
A query was run and no Result Maps were found for the Mapped Statement
A query was run and no Result Maps were found for the Mapped Statement
229 0
|
SQL 关系型数据库 MySQL
[Err] 1294 - Invalid ON UPDATE clause for 'comment_time' column【详细解决办法】
[Err] 1294 - Invalid ON UPDATE clause for 'comment_time' column【详细解决办法】
745 0
[Err] 1294 - Invalid ON UPDATE clause for 'comment_time' column【详细解决办法】
|
数据库
Incorrect result size: expected 1, actual 2
Incorrect result size: expected 1, actual 2
919 0
|
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累积下来也是一笔财富。
3736 0
错误Batch update returned unexpected row count from update [0]; actual row count: 0;

热门文章

最新文章