Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to I tell a Springdata-repository's delete method to not throw an exception if an entity does not exists?

I am using SpringData's repository. If I try to delete an entity via an ID which does not exist or never existed it throws an exception. Since I do not want to check whether the entity exists before I delete it, it would be nice that it would fail silently. It would make it easier because the observable behavior is the same - after the call the entity does not exists anymore. Whether it has been deleted or never existed, I do not care.

Is there a way to modify default behavior of delete(EntityId) so it won't throw an exception, if entity does not exsist?

Documentation of SpringData's delete says that it will throw an exception if an entity does not exist.

like image 544
EhmKah a.k.a. Michael Krauße Avatar asked Aug 05 '16 09:08

EhmKah a.k.a. Michael Krauße


People also ask

What is the return type of delete method in JPA repository?

A derived delete query must start with deleteBy, followed by the name of the selection criteria. These criteria must be provided in the method call. The return value, of type long, indicates how many records the method deleted. Persisting and deleting objects in JPA requires a transaction.

Should I use JpaRepository or CrudRepository?

Crud Repository doesn't provide methods for implementing pagination and sorting. JpaRepository ties your repositories to the JPA persistence technology so it should be avoided. We should use CrudRepository or PagingAndSortingRepository depending on whether you need sorting and paging or not.

Which method is used for delete operation in spring?

The delete() method is used to delete a single entity which we pass as request data and it is available in CrudRepository interface. The CrudRepository extends Repository interface. In Spring Data JPA Repository is top-level interface in hierarchy.


1 Answers

Updated Answer (after downvotes)

My original answer (below) is actually wrong: my understanding of the question was influenced also by the missing reference to the EmptyResultDataAccessException in the official JavaDoc (as reported by Adrian Baker in his comment).

So a better solution to this issue could be the one suggested by Yamashiro Rion

if (repository.existsById(entityId)) {     repository.deleteById(entityId); } 

or this one (without the if, but probably worse performing):

repository.findById(entityId)     .map(repository::delete) 

Original (Wrong) Answer

JavaDocs says that an IllegalArgumentException will be thrown if the provided argument (id, entity, Iterable<T>) is null and not if entity does not exsits.

If you need to avoid the IllegalArgumentException you could implement a custom delete method that checks id != null:

public void customDelete(ID id) {     if(id != null){         this.delete(id);     } } 

Take a look to this docs section if you don't know how to add "Custom implementations for Spring Data repositories"

like image 138
davioooh Avatar answered Sep 20 '22 05:09

davioooh