简体   繁体   中英

Should I check if Entity exists before getting/deleting with Spring data JPA?

Spring data jpa has some Exceptions that it throws, like EntityNotFoundException, but they are not descriptive enough and I though it would be good to check if Entity exists, if not - throw an Exception with a nice description. Is that a good decision or is it just a redundant SQL query and it is better to stick stick to Spring data jpa exceptions?

Jpa Repository has been adapted to work with optionals as well. In that case EntityNotFoundException is not thrown.

ex Optional<User> findByName(String name)

So just check if optional isPresent() and that will be enough

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM