Count Vs Exists (ARCH4063)
Ce contenu n’est pas encore disponible dans votre langue.
Key java.orm.count-vs-exists · Severity info · Category orm
Using count() > 0 to test for presence counts all matches; exists() stops at the first row. Use existsBy... for existence tests.
Incorrect
Section titled “Incorrect”if (orderRepository.countByUserId(userId) > 0) { // counts them all}Correct
Section titled “Correct”if (orderRepository.existsByUserId(userId)) { // short-circuits at the first row}Use a short-circuiting exists() query instead of count > 0 for existence checks.