{"record":{"id":"57e6da4e62952348","repo":"quarkusio/quarkus","slug":"method-of-repository-repository-is-meant-to","errorCode":null,"errorMessage":"${method} of Repository ${repository} is meant to be a count query and can therefore only have a long return type","messagePattern":"(.+?) of Repository (.+?) is meant to be a count query and can therefore only have a long return type","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"extensions/spring-data-jpa/deployment/src/main/java/io/quarkus/spring/data/deployment/generate/DerivedMethodsAdder.java","lineNumber":294,"sourceCode":"                                    throw new IllegalArgumentException(\n                                            method.name() + \" of Repository \" + repositoryClassInfo\n                                                    + \" can only use interfaces to map results to non-entity types.\");\n                                }\n                            }\n                        }\n\n                        DotName effectiveReturnTypeName = finalReturnType.kind() == Type.Kind.TYPE_VARIABLE ? DotNames.OBJECT\n                                : finalReturnType.name();\n\n                        generateFindQueryResultHandling(bc, panacheQuery, finalPageableParameterIndex, params,\n                                repositoryClassInfo, entityClassInfo, effectiveReturnTypeName, parseResult.getTopCount(),\n                                method.name(), customResultTypeName,\n                                entityClassInfo.name().toString(), elementTypeToCast);\n\n                    } else if (parseResult.getQueryType() == MethodNameParser.QueryType.COUNT) {\n                        if (!DotNames.PRIMITIVE_LONG.equals(returnType.name())\n                                && !DotNames.LONG.equals(returnType.name())) {\n                            throw new IllegalArgumentException(\n                                    method.name() + \" of Repository \" + repositoryClassInfo\n                                            + \" is meant to be a count query and can therefore only have a long return type\");\n                        }\n                        if ((finalSortParameterIndex != null) || finalPageableParameterIndex != null) {\n                            throw new IllegalArgumentException(\n                                    method.name() + \" of Repository \" + repositoryClassInfo\n                                            + \" is meant to be a count query and therefore doesn't \" +\n                                            \"support Pageable and Sort method parameters\");\n                        }\n\n                        // call JpaOperations.count()\n                        Expr count = bc.invokeVirtual(\n                                MethodDesc.of(AbstractManagedJpaOperations.class, \"count\", long.class,\n                                        Class.class, String.class, Object[].class),\n                                ops, entityClass,\n                                Const.of(parseResult.getQuery()), paramsArray);\n\n                        handleLongReturnValue(bc, count, returnType.name());","sourceCodeStart":276,"sourceCodeEnd":312,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/extensions/spring-data-jpa/deployment/src/main/java/io/quarkus/spring/data/deployment/generate/DerivedMethodsAdder.java#L276-L312","documentation":"Derived methods whose name starts with countBy are generated as JPA count queries. A count can only be represented as a long, so the spring-data-jpa extension requires the return type to be long or java.lang.Long; anything else (int, boolean, entity, etc.) fails the build with this IllegalArgumentException.","triggerScenarios":"Declaring e.g. int countByActive(boolean active) or Long-streaming wrappers like Optional<Long> countBy... — any countBy method whose returnType.name() is neither DotNames.PRIMITIVE_LONG nor DotNames.LONG.","commonSituations":"Using int as a habitual return type for counts; copying a repository from another codebase where the count method returned Integer; changing a findBy method into countBy without updating the return type.","solutions":["Change the return type to primitive long","Or use java.lang.Long if a boxed type or generic/null-tolerant usage is needed","If a different shape is required (e.g. a results summary DTO), rename the method away from countBy and use @Query instead"],"exampleFix":"// before\nint countByActive(boolean active);\n\n// after\nlong countByActive(boolean active);","handlingStrategy":"validation","validationCode":"// Ensure every countBy method returns long or Long\nfor (Method m : PersonRepository.class.getDeclaredMethods()) {\n    if (m.getName().startsWith(\"countBy\")) {\n        Class<?> r = m.getReturnType();\n        if (r != long.class && r != Long.class)\n            throw new IllegalStateException(m.getName() + \" must return long/Long, got \" + r);\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always declare countBy methods as returning primitive long","Never reuse findBy signatures by merely renaming the prefix","Add an ArchUnit/architectural test enforcing count return types"],"tags":["spring-data","jpa","quarkus","derived-query","build-time"],"backgroundTag":"invalid-count-return-type","analyzedSha":"e1c734241f34c7919086ceb4c9262b4a58f6de44","analyzedAt":"2026-09-05T17:01:29.979Z","contentChangedAt":"2026-09-05T17:01:29.979Z","schemaVersion":2},"datasetVersion":"2026-09-12T22:17:10.623Z"}