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)


目录
相关文章
|
5月前
|
druid Java
Error attempting to get column ‘createTime‘ from result set的异常
Error attempting to get column ‘createTime‘ from result set的异常
349 0
|
数据库 OceanBase
LIMIT_ROW_COUNT
LIMIT_ROW_COUNT
89 1
|
数据库
Data truncation: Incorrect date value: ‘2022-11-28T16:00:00.000Z‘ for column ‘start_date‘ at row 1
Data truncation: Incorrect date value: ‘2022-11-28T16:00:00.000Z‘ for column ‘start_date‘ at row 1
249 0
|
SQL 关系型数据库 MySQL
[Err] 1294 - Invalid ON UPDATE clause for 'comment_time' column【详细解决办法】
[Err] 1294 - Invalid ON UPDATE clause for 'comment_time' column【详细解决办法】
700 0
[Err] 1294 - Invalid ON UPDATE clause for 'comment_time' column【详细解决办法】
成功解决ParserError: Error tokenizing data. C error: Expected 2 fields in line 53, saw 3
成功解决ParserError: Error tokenizing data. C error: Expected 2 fields in line 53, saw 3
|
数据库
Incorrect result size: expected 1, actual 2
Incorrect result size: expected 1, actual 2
856 0
Unsafe query: ‘Update‘ statement without ‘where‘ updates all table rows at once
Unsafe query: ‘Update‘ statement without ‘where‘ updates all table rows at once
664 0
成功解决DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change
成功解决DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change