{"record":{"id":"ff82e12f9e2817b1","repo":"quarkusio/quarkus","slug":"incorrect-order-of-results-ff82e1","errorCode":null,"errorMessage":"Incorrect order of results","messagePattern":"Incorrect order of results","errorType":"http","errorClass":"RuntimeException","httpStatus":500,"severity":"error","filePath":"integration-tests/jpa-mssql/src/main/java/io/quarkus/it/jpa/mssql/JPAFunctionalityTestEndpoint.java","lineNumber":59,"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":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/integration-tests/jpa-mssql/src/main/java/io/quarkus/it/jpa/mssql/JPAFunctionalityTestEndpoint.java#L41-L77","documentation":"After asserting the count, the endpoint checks the first row of the name-ordered CriteriaQuery result is 'Gizmo' (alphabetically first of Gizmo/Quarkus/Microsoft); otherwise it throws this RuntimeException. It verifies ORDER BY translation to the actual SQL Server ordering behaves as expected.","triggerScenarios":"Criteria query with orderBy(cb.asc(from.get(\"name\"))) returns a first element whose name is not 'Gizmo' — wrong row order, collation differences, or wrong/unmapped 'name' attribute ordering.","commonSituations":"MSSQL collation (case-insensitive/custom) changing expected order; duplicate rows shifting order; the Person.name mapping pointing at the wrong column; stale seed data.","solutions":["Confirm clean data set (exactly the three seeded persons) before asserting order","Check the database collation for the name column (e.g. use a binary/case-sensitive collation if needed)","Verify the 'name' attribute maps to the intended column in the Person entity","Clean the table between runs so ordering assertions are deterministic"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"List<Person> all = em.createQuery(\"select p from Person p order by p.name\", Person.class).getResultList();\nif (!all.isEmpty() && !all.get(0).getName().equals(\"Gizmo\")) {\n    throw new IllegalStateException(\"Unexpected first row: \" + all.get(0).getName());\n}","typeGuard":null,"tryCatchPattern":"try {\n    Person first = allpersons.get(0);\n} catch (RuntimeException e) {\n    // dump names + DB collation when order assertion fails\n}","preventionTips":["Seed deterministic data (unique names) before ordering assertions","Check MSSQL column collation affecting ORDER BY","Map Person.name to the intended column","Clean stale rows so ordering is stable"],"tags":["jpa","criteria-query","ordering","mssql"],"backgroundTag":"unexpected-query-result","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"}