quarkusio/quarkus · error · IllegalArgumentException

${method} of Repository ${repository} contains both a Sort p

Error message

${method} of Repository ${repository} contains both a Sort parameter and a sort operation

What it means

Quarkus's spring-data-jpa extension generates implementations for derived query methods at build time. When a derived SELECT method both embeds an 'OrderBy...' clause in its name AND declares an org.springframework.data.domain.Sort parameter, the sort order is ambiguous, so the extension fails the build with this IllegalArgumentException. Only one sort mechanism may be used per method.

Source

Thrown at extensions/spring-data-jpa/deployment/src/main/java/io/quarkus/spring/data/deployment/generate/DerivedMethodsAdder.java:182

                    params[i] = mc.parameter("p" + i);
                }

                mc.body(bc -> {
                    // Store static field and instance field in LocalVars so they can be reused
                    LocalVar ops = bc.localVar("ops", bc.getStaticField(operationsField));
                    LocalVar entityClass = bc.localVar("entityClass",
                            bc.get(mc.this_().field(entityClassFieldDescriptor)));

                    // Build params array for query parameters
                    LocalVar paramsArray = bc.localVar("paramsArray",
                            bc.newEmptyArray(Object.class, parseResult.getParamCount()));
                    for (int i = 0; i < queryParameterIndexes.size(); i++) {
                        bc.set(paramsArray.elem(i), params[queryParameterIndexes.get(i)]);
                    }

                    if (parseResult.getQueryType() == MethodNameParser.QueryType.SELECT) {
                        if (parseResult.getSort() != null && finalSortParameterIndex != null) {
                            throw new IllegalArgumentException(
                                    method.name() + " of Repository " + repositoryClassInfo + " contains both a "
                                            + DotNames.SPRING_DATA_SORT + " parameter and a sort operation");
                        }

                        // ensure that Sort is correctly handled whether it's specified in the method name or via a Sort method param
                        String finalQuery = parseResult.getQuery();
                        Expr sort = Const.ofNull(ClassDesc.of(io.quarkus.panache.common.Sort.class.getName()));
                        if (finalSortParameterIndex != null) {
                            sort = bc.invokeStatic(
                                    MethodDesc.of(TypesConverter.class, "toPanacheSort",
                                            io.quarkus.panache.common.Sort.class,
                                            org.springframework.data.domain.Sort.class),
                                    params[finalSortParameterIndex]);
                        } else if (parseResult.getSort() != null) {
                            finalQuery += PanacheJpaUtil.toOrderBy(parseResult.getSort());
                        } else if (finalPageableParameterIndex != null) {
                            Expr pageable = params[finalPageableParameterIndex];
                            Expr pageableSort = bc.invokeInterface(

View on GitHub (pinned to e1c734241f)

Solutions

  1. Remove the OrderBy clause from the method name and rely on the Sort parameter: findByLastName(String lastName, Sort sort)
  2. Or remove the Sort parameter and keep the name-based ordering: findByLastNameOrderByFirstNameAsc(String lastName)
  3. If both static and dynamic ordering are truly needed, define two separate repository methods

Example fix

// before
List<Person> findByLastNameOrderByFirstNameAsc(String lastName, Sort sort);

// after
List<Person> findByLastName(String lastName, Sort sort);
Defensive patterns

Strategy: validation

Validate before calling

// At build/startup, scan repository interfaces for SELECT methods having both OrderBy in the name and a Sort param
for (Method m : PersonRepository.class.getMethods()) {
    boolean nameSort = m.getName().contains("OrderBy");
    boolean paramSort = Arrays.stream(m.getParameterTypes()).anyMatch(Sort.class::isAssignableFrom);
    if (nameSort && paramSort)
        throw new IllegalStateException("Ambiguous sort in " + m.getName());
}

Prevention

When it happens

Trigger: Declaring a repository method like List<Person> findByLastNameOrderByFirstNameAsc(String lastName, Sort sort) — i.e. MethodNameParser.QueryType.SELECT with parseResult.getSort() != null and a Sort parameter (finalSortParameterIndex != null) present at the same time.

Common situations: Incrementally adding paging to an existing repository: a method already named findByXOrderByYAsc gets a Sort argument appended for 'dynamic sorting'; copying Spring Data examples that allow this combination without realizing Quarkus's derived-method generation is stricter.

Related errors


AI-assisted analysis of quarkusio/quarkus@e1c734241f (2026-09-05). Data as JSON: /api/errors/295fb7ee93004f2d. Report an issue: GitHub.