Closed
Description
The implementation of CrudRepository.deleteById throws an EmptyResultDataAccessException when no entity for the id exists. This is not described in the javadoc.
/**
* Deletes the entity with the given id.
*
* @param id must not be {@literal null}.
* @throws IllegalArgumentException in case the given {@literal id} is {@literal null}
*/
void deleteById(ID id);
See implementation of the interface https://github.com/spring-projects/spring-data-jpa/blob/main/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java#L164-L172
@Transactional
@Override
public void deleteById(ID id) {
Assert.notNull(id, ID_MUST_NOT_BE_NULL);
delete(findById(id).orElseThrow(() -> new EmptyResultDataAccessException(
String.format("No %s entity with id %s exists", entityInformation.getJavaType(), id), 1)));
}
The EmptyResultDataAccessException
is not part of spring-data-commons. So maybe that's the reason it's not mentioned here.
But can we add a hint to this behavior somehow? From the current javadoc I would expect no exception if no entity was found, similar to a sql deletion statement.