-
Notifications
You must be signed in to change notification settings - Fork 683
JavaDoc of CrudRepository.deleteById is too unspecific #3280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
schauder
added a commit
that referenced
this issue
Apr 29, 2025
The documentation now clarifies that entity might get loaded and therefore possibly OptimisticLockingFailureException might get thrown. Closes #3280
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Spring JPA's
CrudRepository
interface contains adeleteById
method with the following documentation:The second sentence is correct but can be easily misinterpreted. When looking at the method signature, one could assume that this method just deletes the corresponding entity on the database by id (equivalent to the JPQL query
delete from X where id = :id
). And if no entry with that identifier exists, it just ignores the call.However, when the deleteById method is called in a transactional context concurrently, it can fail with an optimistic lock exception. The reason being that the underlying
SimpleJpaRepository
that implements that interface method is actually doing this:findById(id).ifPresent(this::delete);
It first loads the entity from the db into the persistence context and then deletes the entity instance if found. If a concurrent thread deletes or modifies the same entity between the find and the delete call, the operation will fail. This behavior is not obvious from reading the JavaDoc on the
deleteById
method. Could you please improve the documentation there? Or maybe improve the implementation and not load the entity from the db if not already within the current persistence context? Thanks!The text was updated successfully, but these errors were encountered: