Skip to content

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

Open
AndreasKasparek opened this issue Apr 25, 2025 · 0 comments · May be fixed by #3281
Open

JavaDoc of CrudRepository.deleteById is too unspecific #3280

AndreasKasparek opened this issue Apr 25, 2025 · 0 comments · May be fixed by #3281
Assignees
Labels
type: documentation A documentation update

Comments

@AndreasKasparek
Copy link

Spring JPA's CrudRepository interface contains a deleteById method with the following documentation:

	/**
	 * Deletes the entity with the given id.
	 * <p>
	 * If the entity is not found in the persistence store it is silently ignored.
	 *
	 * @param id must not be {@literal null}.
	 * @throws IllegalArgumentException in case the given {@literal id} is {@literal null}
	 */
	void deleteById(ID id);

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!

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Apr 25, 2025
@mp911de mp911de added type: documentation A documentation update and removed status: waiting-for-triage An issue we've not yet triaged labels Apr 28, 2025
@schauder schauder transferred this issue from spring-projects/spring-data-jpa Apr 29, 2025
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
@schauder schauder linked a pull request Apr 29, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: documentation A documentation update
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants