简体   繁体   中英

spring-jpa how to get EntityNotFoundException or similar on find* query methods?

我正在解决以下问题:如果数据库中没有对象,我不想获取Null引用。

All the JPA providers I have tried will return an empty list if there are no matches to the query.

Also,

CollectionUtils.isNotEmpty(resultList);

Is your friend.

Is it allowed -in your project to use getReference() method? If yes, then please use it to avoid null reference being returned.

getReference(Class<T>,Object) throws an EntityNotFoundException if it cant find the entity being searched for.

Or else, if you has to use find() method, then you may handle null manually ie with the help of if..else block. etc.

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