{"record":{"id":"2d5c7b1ec7796d88","repo":"quarkusio/quarkus","slug":"wrong-result-from-named-jpa-query","errorCode":null,"errorMessage":"Wrong result from named JPA query","messagePattern":"Wrong result from named JPA query","errorType":"http","errorClass":"RuntimeException","httpStatus":500,"severity":"error","filePath":"integration-tests/jpa-mariadb/src/main/java/io/quarkus/it/jpa/mariadb/JPAFunctionalityTestEndpoint.java","lineNumber":91,"sourceCode":"        //Try to use prepared statement with setObject:\n        try (PreparedStatement ps = ds.getConnection().prepareStatement(\"select * from Person as p where p.name = ?\")) {\n            ps.setObject(1, \"Quarkus\");\n            final ResultSet resultSet = ps.executeQuery();\n\n            if (!resultSet.next()) {\n                throw new RuntimeException(\"Person Quarkus doesn't exist when it should\");\n            }\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\");\n            }\n        });\n\n        //Check that HQL fetch does not throw an exception\n        QuarkusTransaction.requiringNew()\n                .run(() -> em.createQuery(\"from Person p left join fetch p.address a\").getResultList());\n\n        cleanUpData();\n\n        return \"OK\";\n    }\n\n    private void cleanUpData() {\n        QuarkusTransaction.requiringNew()\n                .run(() -> em.createNativeQuery(\"Delete from Person\").executeUpdate());\n    }\n\n    private void persistNewPerson(String name) {","sourceCodeStart":73,"sourceCodeEnd":109,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/integration-tests/jpa-mariadb/src/main/java/io/quarkus/it/jpa/mariadb/JPAFunctionalityTestEndpoint.java#L73-L109","documentation":"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).","triggerScenarios":"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.","commonSituations":"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.","solutions":["Run cleanUpData before seeding to remove duplicate rows","Inspect the @NamedQuery definition for 'get_person_by_name' and correct its WHERE clause","Check the MariaDB table contents directly for unexpected rows","Verify Person.name column mapping matches the queried column"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"List<Person> matches = em.createNamedQuery(\"get_person_by_name\", Person.class)\n        .setParameter(\"name\", \"Quarkus\").getResultList();\nif (matches.size() != 1) throw new IllegalStateException(\"Expected exactly 1 Person named Quarkus, got \" + matches.size());","typeGuard":null,"tryCatchPattern":"try {\n    Person p = typedQuery.getSingleResult();\n} catch (RuntimeException e) {\n    // inspect getResultList() for duplicates/stale rows, clean data, retry\n}","preventionTips":["Clean table before seeding to prevent duplicate matches","Assert with getResultList() and size checks to ease debugging","Keep @NamedQuery WHERE clauses aligned with the mapped column","Use unique constraints on test fixture columns"],"tags":["jpa","named-query","mariadb","integration-test"],"backgroundTag":"unexpected-query-result","analyzedSha":"e1c734241f34c7919086ceb4c9262b4a58f6de44","analyzedAt":"2026-09-05T17:01:29.979Z","contentChangedAt":"2026-09-05T17:01:29.979Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}