How to throw exceptions on search methods in spring data jpa
Solution 1
You just need orElseThrow
orderRepository.findByCustomerAndPayment(customer, payment).orElseThrow(() -> new ResourceNotFoundException("customer", "id", customer.getId()));
Solution 2
If you're using Java 8, you can use Optional<Order>
as the return type of the repository method. If the repository method returns an empty Optional
calling get on it will throw a NoSuchElementException
. Otherwise there is no support for throwing exceptions by repository methods if there are no results.
try {
Optional<Order> result = repository.findByCustomerAndPayment(customer,payment);
Order order = result.get();
} catch(NoSuchElementException e) {
// do something with the exception
}
Solution 3
You can make custom repository implementation like below:
public interface OrderRepositoryCustom {
Order findByCustomerAndPaymentRequired(Customer customer, Payment payment);
}
public class OrderRepositoryImpl implements OrderRepositoryCustom {
@Autowired
OrderRepository orderRepository;
@Override
public Order findByCustomerAndPaymentRequired(Customer customer, Payment payment) {
Order o = orderRepository.findByCustomerAndPayment(customer, payment);
if(o == null) {
throw new IncorrectResultSizeDataAccessException(1);
}
return o;
}
}
Your OrderRepository
interface should extend customized:
public interface OrderRepository extends CrudRepository<Order, Long>, OrderRepositoryCustom {
Order findByCustomerAndPayment(Customer customer, Payment payment);
}
Edited
As IncorrectResultSizeDataAccessException
is RuntimeException
, then no need to throws
declaration - i fixed that.
SST
Updated on June 14, 2022Comments
-
SST almost 2 years
I am using spring-data-jpa repositories for database operations. I want to throw exceptions if object doesn't exists in database for all methods in my repositories. For example Consider the following method in
OrderRepository
findByCustomerAndPayment(Customer customer, Payment payment);
I want to query all orders based on customerId and paymentId. Both the objects are neccessry in the above query. But spring-data-rest returns null if I gave cutomerId doesn't exists in database. I expect spring-data-rest to throw exception if object doesn't exists in database.
How to achieve this?
-
rpajaziti about 4 yearsThis is the best solution i think. First devclare the method as Optional<{Type}> and then use this. Thanks Salah.
-
sigur over 3 yearsHandling RuntimeExceptions should not be a good way, especially if you can avoid them. Optional class has different methods to check and to perform actions if element is missing or it's present. Example: order.orElseThrow(...), order.orElse(...), order.ifPresent(), etc...
-
ghossio almost 3 yearsI think the same but, anyone knows the opposite? Throwing a exception if exists?
-
hocikto over 2 years@ghossio bit late to party - why would you want it? only thing I can think of right now is having a count and conditionally throwing exception but there is no usage of optional if that was what you needed