您可以自定義的倉庫實現象下面這樣:
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;
}
}
你OrderRepository
接口應該擴展定製:
public interface OrderRepository extends CrudRepository<Order, Long>, OrderRepositoryCustom {
Order findByCustomerAndPayment(Customer customer, Payment payment);
}
編輯
由於IncorrectResultSizeDataAccessException
是RuntimeException
,那麼不需要throws
聲明 - 我解決了這個問題。