{"record":{"id":"f325a3f9e71b041f","repo":"quarkusio/quarkus","slug":"incorrect-order-of-results-f325a3","errorCode":null,"errorMessage":"Incorrect order of results","messagePattern":"Incorrect order of results","errorType":"http","errorClass":"RuntimeException","httpStatus":500,"severity":"error","filePath":"integration-tests/jpa-mysql/src/main/java/io/quarkus/it/jpa/mysql/JPAFunctionalityTestEndpoint.java","lineNumber":55,"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":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/integration-tests/jpa-mysql/src/main/java/io/quarkus/it/jpa/mysql/JPAFunctionalityTestEndpoint.java#L37-L73","documentation":"A test assertion in the jpa-mysql integration endpoint verifying that a Criteria query ordered by cb.asc(from.get(\"name\")) returns rows sorted alphabetically, with 'Gizmo' first. The database or the ORDER BY translation did not yield the expected ordering.","triggerScenarios":"Running the criteria query with orderBy(name asc) when the first returned Person's name is not 'Gizmo' — caused by missing/ignored ORDER BY, wrong collation, or unexpected seed data names.","commonSituations":"Dialect/collation differences in MySQL affecting sort order, seed rows inserted with different names than expected, or a JPQL/Criteria translation bug.","solutions":["Log the returned names in order to see the actual sort result","Verify the seed data contains exactly the expected names including 'Gizmo'","Confirm the orderBy clause is included in the generated SQL (enable SQL logging)","Check MySQL column collation for case-sensitivity surprises"],"exampleFix":"// before\ncq.select(from).orderBy(cb.asc(from.get(\"name\")));\n// after (diagnostic)\nList<Person> allpersons = q.getResultList();\nallpersons.forEach(p -> System.out.println(p.getName())); // confirm actual order\ncq.select(from).orderBy(cb.asc(from.get(\"name\")));","handlingStrategy":"validation","validationCode":"List<String> names = allpersons.stream().map(Person::getName).toList();\nif (!names.equals(names.stream().sorted().toList())) {\n    throw new IllegalStateException(\"Results not sorted by name: \" + names);\n}","typeGuard":null,"tryCatchPattern":"try {\n    q.getResultList();\n} catch (RuntimeException e) {\n    throw new AssertionError(\"ordered criteria query failed: \" + e.getMessage(), e);\n}","preventionTips":["Enable SQL logging to confirm ORDER BY is generated","Verify seed data names before asserting order","Check database collation settings for case-sensitivity"],"tags":["jpa","mysql","sorting","criteria-api","integration-test"],"backgroundTag":"jpa-query-order-mismatch","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"}