{"record":{"id":"e5337e8de031df30","repo":"quarkusio/quarkus","slug":"incorrect-order-of-results-e5337e","errorCode":null,"errorMessage":"Incorrect order of results","messagePattern":"Incorrect order of results","errorType":"http","errorClass":"RuntimeException","httpStatus":500,"severity":"error","filePath":"integration-tests/jpa-postgresql-withxml/src/main/java/io/quarkus/it/jpa/postgresql/JPAFunctionalityTestEndpoint.java","lineNumber":84,"sourceCode":"            persistNewPerson(\"Gizmo\");\n            persistNewPerson(\"Quarkus\");\n            persistNewPerson(\"Hibernate ORM\");\n        });\n\n        //Load all persons and run some checks on the query results:\n        QuarkusTransaction.requiringNew().run(() -> {\n            CriteriaBuilder cb = em.getCriteriaBuilder();\n\n            CriteriaQuery<Person> cq = cb.createQuery(Person.class);\n            Root<Person> from = cq.from(Person.class);\n            cq.select(from).orderBy(cb.asc(from.get(\"name\")));\n            TypedQuery<Person> q = em.createQuery(cq);\n            List<Person> allpersons = q.getResultList();\n            if (allpersons.size() != 3) {\n                throw new RuntimeException(\"Incorrect number of results\");\n            }\n            if (!allpersons.get(0).getName().equals(\"Gizmo\")) {\n                throw new RuntimeException(\"Incorrect order of results\");\n            }\n            StringBuilder sb = new StringBuilder(\"list of stored Person names:\\n\\t\");\n            for (Person p : allpersons) {\n                p.describeFully(sb);\n            }\n            sb.append(\"\\nList complete.\\n\");\n            System.out.print(sb);\n        });\n\n        //Try a JPA named query:\n        QuarkusTransaction.requiringNew().run(() -> {\n            TypedQuery<Person> typedQuery = em.createNamedQuery(\n                    \"get_person_by_name\", Person.class);\n            typedQuery.setParameter(\"name\", \"Quarkus\");\n            final Person singleResult = typedQuery.getSingleResult();\n\n            if (!singleResult.getName().equals(\"Quarkus\")) {\n                throw new RuntimeException(\"Wrong result from named JPA query\");","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/integration-tests/jpa-postgresql-withxml/src/main/java/io/quarkus/it/jpa/postgresql/JPAFunctionalityTestEndpoint.java#L66-L102","documentation":"A RuntimeException from the same smoke test asserting that a Criteria query ordered by name (ascending) returns 'Gizmo' as the first result. It fires when the result set is correctly sized but incorrectly ordered, meaning the ORDER BY was not applied or data differs from expectations.","triggerScenarios":"CriteriaQuery with cq.orderBy(cb.asc(from.get(\"name\"))) returns rows whose first element's name is not 'Gizmo' - e.g. different seed data or collation differences.","commonSituations":"Database with case-sensitive collation altering sort order, seed data changed, or DB-level ordering differences across locales when running against a locally configured PostgreSQL.","solutions":["Check the seeded Person names in the test dataset match the expectation (Gizmo, Quarkus, ...)","Verify the ORDER BY expression is applied (inspect generated SQL with logging)","Check database collation/encoding (e.g. LC_COLLATE) matches the environment the test was written for"],"exampleFix":"// before\ncq.select(from).orderBy(cb.asc(from.get(\"name\")));\n// after (collation-proof check)\ncq.select(from).orderBy(cb.asc(cb.lower(from.get(\"name\"))));","handlingStrategy":"validation","validationCode":"java.util.Comparator<Person> cmp = java.util.Comparator.comparing(Person::getName, String.CASE_INSENSITIVE_ORDER);\nboolean sorted = allpersons.stream().sorted(cmp).toList().equals(allpersons);\nboolean firstIsGizmo = !allpersons.isEmpty() && \"Gizmo\".equals(allpersons.get(0).getName());","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Log generated SQL to confirm ORDER BY is present","Use case-insensitive comparisons when collation may vary","Keep seed data names deterministic and documented"],"tags":["jpa","criteria-api","ordering","test-assertion"],"backgroundTag":"unexpected-query-result-order","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"}