Skip to content

IncorrectUpdateSemanticsDataAccessException: Id not found when saving an unchanged Entity #2176

@joseph415

Description

@joseph415

Environment:

  • Spring Boot: 3.3.4
  • Spring Data JDBC: 3.3.4
  • Database: MySQL 8.0.28 (InnoDB)

Description

In our service, we accidentally encountered a case where save() was called on an entity that had no modified fields.

Because the entity data was identical to the data in the database, the MySQL (InnoDB) engine performed an optimization and reported 0 for the number of affected rows from the UPDATE statement.

This 0 affected rows count subsequently caused Spring Data JDBC to throw an exception:
Caused by: org.springframework.dao.IncorrectUpdateSemanticsDataAccessException: Failed to update entity [...]; Id [...] not found in database

The UPDATE statement (from DefaultDataAccessStrategy.java) returns 0 affected rows:
https://github.com/spring-projects/spring-data-relational/blob/main/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/DefaultDataAccessStrategy.java#L155

This then triggers the exception here (in JdbcAggregateChangeExecutionContext.java):
https://github.com/spring-projects/spring-data-relational/blob/main/spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/JdbcAggregateChangeExecutionContext.java#L350

This appears to be a bug, because the entity does exist in the database; it just wasn't modified by the UPDATE statement. Is it possible to correct this behavior so that an UPDATE with 0 affected rows on an existing entity does not throw an exception?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions