quarkusio/quarkus · error · RuntimeException

Wrong result from named JPA query

Error message

Wrong result from named JPA query

What it means

After fetching Person 'Quarkus' via the named query 'get_person_by_name', the endpoint asserts the returned entity's name equals 'Quarkus'; a mismatch throws this RuntimeException. It indicates the named query matched a row but returned different/unexpected data (or entity state is corrupted/mismapped).

Source

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

        //Try to use prepared statement with setObject:
        try (PreparedStatement ps = ds.getConnection().prepareStatement("select * from Person as p where p.name = ?")) {
            ps.setObject(1, "Quarkus");
            final ResultSet resultSet = ps.executeQuery();

            if (!resultSet.next()) {
                throw new RuntimeException("Person Quarkus doesn't exist when it should");
            }
        }

        //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 Person").executeUpdate());
    }

    private void persistNewPerson(String name) {

View on GitHub (pinned to e1c734241f)

Solutions

  1. Run cleanUpData before seeding to remove duplicate rows
  2. Inspect the @NamedQuery definition for 'get_person_by_name' and correct its WHERE clause
  3. Check the MariaDB table contents directly for unexpected rows
  4. Verify Person.name column mapping matches the queried column
Defensive patterns

Strategy: try-catch

Validate before calling

List<Person> matches = em.createNamedQuery("get_person_by_name", Person.class)
        .setParameter("name", "Quarkus").getResultList();
if (matches.size() != 1) throw new IllegalStateException("Expected exactly 1 Person named Quarkus, got " + matches.size());

Try / catch

try {
    Person p = typedQuery.getSingleResult();
} catch (RuntimeException e) {
    // inspect getResultList() for duplicates/stale rows, clean data, retry
}

Prevention

When it happens

Trigger: NamedQuery 'get_person_by_name' returns a Person whose getName() != 'Quarkus' — e.g. the query filters on the wrong column, multiple rows exist and getSingleResult picked a stale/different row, or seed data contains another row.

Common situations: Duplicate 'Quarkus' rows from re-runs without cleanup; @NamedQuery SQL mapping name to the wrong column; charset/collation issues on MariaDB making a different row match; leftover data from a previous test execution.

Related errors


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