quarkusio/quarkus · error · RuntimeException

Wrong result from named JPA query

Error message

Wrong result from named JPA query

What it means

Postgres variant of the named-query assertion: createNamedQuery("get_person_by_name") with name='Quarkus' returned an entity whose name is not 'Quarkus'. The @NamedQuery on Person or the underlying data is inconsistent with the test's expectation.

Source

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

                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");
            }
        });

        //Check that HQL fetch does not throw an exception
        QuarkusTransaction.requiringNew()
                .run(() -> em.createQuery("from Person p left join fetch p.address a").getResultList());

        cleanUpData();

        return "OK";
    }

    private void cleanUpData() {
        QuarkusTransaction.requiringNew()
                .run(() -> em.createNativeQuery("Delete from myschema.Person").executeUpdate());
    }

    private void persistNewPerson(String name) {

View on GitHub (pinned to e1c734241f)

Solutions

  1. Verify the @NamedQuery JPQL on the Person entity
  2. Re-seed the 'Quarkus' Person inside a committed transaction before querying
  3. Inspect the returned entity to identify which row matched
  4. Clean the person table before the test for determinism

Example fix

// before
@NamedQuery(name = "get_person_by_name", query = "select p from Person p where p.name = :name")
// after (ensure commit before query)
QuarkusTransaction.requiringNew().run(() -> em.persist(new Person("Quarkus", ...)));
TypedQuery<Person> typedQuery = em.createNamedQuery("get_person_by_name", Person.class);
Defensive patterns

Strategy: validation

Validate before calling

List<Person> matches = em.createNamedQuery("get_person_by_name", Person.class)
        .setParameter("name", "Quarkus").getResultList();
if (matches.size() != 1 || !"Quarkus".equals(matches.get(0).getName())) {
    throw new IllegalStateException("NamedQuery returned: " + matches);
}

Try / catch

try {
    typedQuery.getSingleResult();
} catch (PersistenceException e) {
    throw new AssertionError("named query failed unexpectedly", e);
}

Prevention

When it happens

Trigger: Running the named query path in the jpa-postgresql base endpoint when the matching row was not stored correctly, the JPQL filter is wrong, or multiple/matching rows resolve to an unexpected entity.

Common situations: Entity @NamedQuery edited out of sync with test data, seeding inside a transaction that never committed, or stale database rows from earlier runs.

Related errors


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