{"record":{"id":"423b48c1c2ea4b63","repo":"quarkusio/quarkus","slug":"incorrect-order-of-results","errorCode":null,"errorMessage":"Incorrect order of results","messagePattern":"Incorrect order of results","errorType":"exception","errorClass":"RuntimeException","httpStatus":500,"severity":"error","filePath":"integration-tests/hibernate-orm-cache/src/main/java/io/quarkus/it/hibernate/orm/cache/HibernateOrmCacheTestEndpoint.java","lineNumber":531,"sourceCode":"        });\n\n        assertRegionStats(new Counts(0, 0, 3, 4), Pokemon.class.getName());\n    }\n\n    private static void listExistingPersons(EntityManager em) {\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        q.setHint(\"org.hibernate.cacheable\", Boolean.TRUE);\n        List<Person> allpersons = q.getResultList();\n        if (allpersons.size() != 4) {\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    private void testReadWrite() {\n        //Store some well known Pokemon instances we can then test on:\n        storeTestPokemons(new Counts(3, 0, 0, 3));\n\n        //Load all persons and run some checks on the cache hits\n        verifyFindByIdPokemons(new int[] { 2555, 3670, 3219 }, new Counts(0, 3, 0, 3));\n\n        //Rebalance cp values for pokemons\n        rebalanceCpsForPokemons(new Counts(3, 3, 0, 3));","sourceCodeStart":513,"sourceCodeEnd":549,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/integration-tests/hibernate-orm-cache/src/main/java/io/quarkus/it/hibernate/orm/cache/HibernateOrmCacheTestEndpoint.java#L513-L549","documentation":"RuntimeException thrown by listExistingPersons when the ordered result list does not start with the Person named 'Gizmo'. The CriteriaQuery applies orderBy(cb.asc(from.get(\"name\"))) and the test expects alphabetical order, so 'Gizmo' must be first; this validates that ordering is preserved and the cached query result is correct.","triggerScenarios":"The first element of the cacheable, name-ordered Person query is not 'Gizmo': data with different names was inserted, the query cache holds a result from a differently-ordered/older dataset, or the DB collation orders names differently than the test expects.","commonSituations":"Leftover rows from previous runs changing the alphabetical first entry; query cache stale after data mutation; locale/collation differences between environments altering string ordering; seed data names changed.","solutions":["Reset the database to the canonical seed dataset before the test so 'Gizmo' is alphabetically first","Invalidate the query cache region so ordering is recomputed from current data","Verify no extra Person rows exist (count check precedes this assertion - fix that first if it fails)","Check DB collation/locale settings that could reorder names"],"exampleFix":"// before\nif (!allpersons.get(0).getName().equals(\"Gizmo\")) {\n    throw new RuntimeException(\"Incorrect order of results\");\n}\n// after\nif (!allpersons.get(0).getName().equals(\"Gizmo\")) {\n    throw new RuntimeException(\"Incorrect order of results: first=\" + allpersons.get(0).getName());\n}","handlingStrategy":"validation","validationCode":"List<String> names = em.createQuery(\"SELECT p.name FROM Person p ORDER BY p.name\", String.class)\n    .setHint(\"org.hibernate.cacheable\", Boolean.FALSE)\n    .getResultList();\nif (!\"Gizmo\".equals(names.isEmpty() ? null : names.get(0)))\n    throw new IllegalStateException(\"First person is not Gizmo: \" + (names.isEmpty() ? \"none\" : names.get(0)));","typeGuard":null,"tryCatchPattern":"try {\n    listExistingPersons();\n} catch (RuntimeException e) {\n    if (e.getMessage().contains(\"Incorrect order of results\")) {\n        // evict query cache region and re-run to recompute ordering\n    }\n}","preventionTips":["Cross-check ordering with an uncached query when cached results look wrong","Ensure DB collation yields the expected alphabetical order","Keep seed names consistent with the test's expected first entry"],"tags":["hibernate-orm","query-cache","ordering","integration-test"],"backgroundTag":"stale-query-cache-result","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"}