Skip to content

deleteById should not throw EmptyResultDataAccessException #2651

Closed
@twonky4

Description

@twonky4

The implementation of CrudRepository.deleteById throws an EmptyResultDataAccessException when no entity for the id exists. This is not described in the javadoc.

See interface https://github.com/spring-projects/spring-data-commons/blob/main/src/main/java/org/springframework/data/repository/CrudRepository.java#L97-L103

	/**
	 * 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.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions