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)


目录
相关文章
|
8天前
|
druid Java
Error attempting to get column ‘createTime‘ from result set的异常
Error attempting to get column ‘createTime‘ from result set的异常
|
10月前
|
程序员 Go API
译|Errors are values
译|Errors are values
50 0
|
11月前
|
数据库
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
180 0
解决sql update 1292 - Truncated incorrect DOUBLE value:
出现这种错误,我属实焦头烂额了一会儿,这个错误基本可以分为以下两种情况: 一:你的字段类型是varchar,但是你这样进行操作,是不对的, UPDATE StuCose SET Cno=60 应该是: UPDATE StuCose SET Cno='60' 二:你更新操作进行子查询时,需要的两个表的相同的字段的类型不同,比如你的cose表中的Cno是int类型, 但是你的stucose表中是varchar类型 ,这个时候类似这样进行了子查询的update就会报错 UPDA.
416 0
解决sql update 1292 - Truncated incorrect DOUBLE value:
成功解决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
731 0
【TP5.1】variable type error: array
【TP5.1】variable type error: array
146 0
【TP5.1】variable type error: array
|
SQL 关系型数据库 MySQL
[Err] 1294 - Invalid ON UPDATE clause for 'comment_time' column【详细解决办法】
[Err] 1294 - Invalid ON UPDATE clause for 'comment_time' column【详细解决办法】
623 0
[Err] 1294 - Invalid ON UPDATE clause for 'comment_time' column【详细解决办法】
成功解决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