quarkusio/quarkus · error · RuntimeException

Incorrect order of results

Error message

Incorrect order of results

What it means

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.

Source

Thrown at integration-tests/jpa-mysql/src/main/java/io/quarkus/it/jpa/mysql/JPAFunctionalityTestEndpoint.java:55

            persistNewPerson("Gizmo");
            persistNewPerson("Quarkus");
            persistNewPerson("Hibernate ORM");
        });

        //Load all persons and run some checks on the query results:
        QuarkusTransaction.requiringNew().run(() -> {
            CriteriaBuilder cb = em.getCriteriaBuilder();

            CriteriaQuery<Person> cq = cb.createQuery(Person.class);
            Root<Person> from = cq.from(Person.class);
            cq.select(from).orderBy(cb.asc(from.get("name")));
            TypedQuery<Person> q = em.createQuery(cq);
            List<Person> allpersons = q.getResultList();
            if (allpersons.size() != 3) {
                throw new RuntimeException("Incorrect number of results");
            }
            if (!allpersons.get(0).getName().equals("Gizmo")) {
                throw new RuntimeException("Incorrect order of results");
            }
            StringBuilder sb = new StringBuilder("list of stored Person names:\n\t");
            for (Person p : allpersons) {
                p.describeFully(sb);
            }
            sb.append("\nList complete.\n");
            System.out.print(sb);
        });

        //Try a JPA named query:
        QuarkusTransaction.requiringNew().run(() -> {
            TypedQuery<Person> typedQuery = em.createNamedQuery(
                    "get_person_by_name", Person.class);
            typedQuery.setParameter("name", "Quarkus");
            final Person singleResult = typedQuery.getSingleResult();

            if (!singleResult.getName().equals("Quarkus")) {
                throw new RuntimeException("Wrong result from named JPA query");

View on GitHub (pinned to e1c734241f)

Solutions

  1. Log the returned names in order to see the actual sort result
  2. Verify the seed data contains exactly the expected names including 'Gizmo'
  3. Confirm the orderBy clause is included in the generated SQL (enable SQL logging)
  4. Check MySQL column collation for case-sensitivity surprises

Example fix

// before
cq.select(from).orderBy(cb.asc(from.get("name")));
// after (diagnostic)
List<Person> allpersons = q.getResultList();
allpersons.forEach(p -> System.out.println(p.getName())); // confirm actual order
cq.select(from).orderBy(cb.asc(from.get("name")));
Defensive patterns

Strategy: validation

Validate before calling

List<String> names = allpersons.stream().map(Person::getName).toList();
if (!names.equals(names.stream().sorted().toList())) {
    throw new IllegalStateException("Results not sorted by name: " + names);
}

Try / catch

try {
    q.getResultList();
} catch (RuntimeException e) {
    throw new AssertionError("ordered criteria query failed: " + e.getMessage(), e);
}

Prevention

When it happens

Trigger: 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.

Common situations: Dialect/collation differences in MySQL affecting sort order, seed rows inserted with different names than expected, or a JPQL/Criteria translation bug.

Related errors


AI-assisted analysis of quarkusio/quarkus@e1c734241f (2026-09-05). Data as JSON: /api/errors/f325a3f9e71b041f. Report an issue: GitHub.