{"record":{"id":"1f38d47ef2a0cbab","repo":"quarkusio/quarkus","slug":"sort-cannot-be-used-with-named-query-add-an-orde","errorCode":null,"errorMessage":"Sort cannot be used with named query, add an \"order by\" clause to the named query \"\" instead","messagePattern":"Sort cannot be used with named query, add an \"order by\" clause to the named query \"\" instead","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"extensions/panache/hibernate-orm-panache-common/runtime/src/main/java/io/quarkus/hibernate/orm/panache/common/runtime/AbstractJpaOperations.java","lineNumber":188,"sourceCode":"    }\n\n    public int paramCount(Map<String, Object> params) {\n        return params != null ? params.size() : 0;\n    }\n\n    //\n    // Queries\n\n    public PanacheQueryType find(Class<?> entityClass, String query, Object... params) {\n        return find(entityClass, query, null, params);\n    }\n\n    public PanacheQueryType find(Class<?> entityClass, String panacheQuery, Sort sort, Object... params) {\n        SessionType session = getSession(entityClass);\n        if (PanacheJpaUtil.isNamedQuery(panacheQuery)) {\n            String namedQuery = panacheQuery.substring(1);\n            if (sort != null) {\n                throw new IllegalArgumentException(\n                        \"Sort cannot be used with named query, add an \\\"order by\\\" clause to the named query \\\"\" + namedQuery\n                                + \"\\\" instead\");\n            }\n            NamedQueryUtil.checkNamedQuery(entityClass, namedQuery);\n            return createPanacheQuery(session, entityClass, panacheQuery, panacheQuery, null, params);\n        }\n\n        String translatedHqlQuery = PanacheJpaUtil.createFindQuery(entityClass, panacheQuery, paramCount(params));\n        return createPanacheQuery(session, entityClass, translatedHqlQuery, panacheQuery, sort, params);\n    }\n\n    public PanacheQueryType find(Class<?> entityClass, String panacheQuery, Map<String, Object> params) {\n        return find(entityClass, panacheQuery, null, params);\n    }\n\n    public PanacheQueryType find(Class<?> entityClass, String panacheQuery, Sort sort, Map<String, Object> params) {\n        SessionType session = getSession(entityClass);\n        if (PanacheJpaUtil.isNamedQuery(panacheQuery)) {","sourceCodeStart":170,"sourceCodeEnd":206,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/extensions/panache/hibernate-orm-panache-common/runtime/src/main/java/io/quarkus/hibernate/orm/panache/common/runtime/AbstractJpaOperations.java#L170-L206","documentation":"When a Panache query string starts with '#', it is a named-query reference. Hibernate applies ordering inside the named query's HQL, so Panache refuses a separate Sort parameter rather than silently ignoring or conflicting with it.","triggerScenarios":"Calling find(entityClass, \"#Person.findByName\", Sort.descending(\"name\"), params) — any find/list/stream call with a '#'-prefixed named query and a non-null Sort.","commonSituations":"Migrating from a dynamic query to a named query and keeping the old Sort argument; helper methods that always pass a Sort default; mixing Spring-Data-like API expectations with Panache named queries.","solutions":["Remove the Sort argument from the call","Add an 'order by' clause inside the named query HQL itself","Use a plain HQL query string instead of the named query if you need dynamic sorting"],"exampleFix":"// before\nrepo.find(\"#Person.getAll\", Sort.descending(\"name\")).list();\n// after: add 'order by name desc' to @NamedQuery and call\nrepo.find(\"#Person.getAll\").list();","handlingStrategy":"validation","validationCode":"if (panacheQuery.startsWith(\"#\") && sort != null) {\n    throw new IllegalArgumentException(\"Remove Sort when using named query \" + panacheQuery);\n}","typeGuard":null,"tryCatchPattern":"try {\n    return find(entityClass, query, sort, params);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"Sort cannot be used with named query\")) {\n        return find(entityClass, query, null, params);\n    }\n    throw e;\n}","preventionTips":["Never pass Sort alongside '#'-prefixed named queries","Put 'order by' inside the named query HQL","Centralize named-query calls in helpers that drop the Sort argument","Document the constraint for teams using generic query helpers"],"tags":["quarkus","panache","named-query","sort"],"backgroundTag":"sort-with-named-query","analyzedSha":"e1c734241f34c7919086ceb4c9262b4a58f6de44","analyzedAt":"2026-09-05T17:01:29.979Z","contentChangedAt":"2026-09-05T17:01:29.979Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}