{"record":{"id":"b1ba53879b8df821","repo":"quarkusio/quarkus","slug":"incorrect-citizen-country-getname-expe","errorCode":null,"errorMessage":"Incorrect citizen: \" + country.getName() + \", expected: \" + expectedName","messagePattern":"Incorrect citizen: \" \\+ country\\.getName\\(\\) \\+ \", expected: \" \\+ expectedName","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":187,"sourceCode":"        counts.put(Citizen.class.getName() + \"##NaturalId\", new Counts(1, 1, 0, 3));\n        updateNaturalId(counts);\n\n        counts = new TreeMap<>();\n        counts.put(Citizen.class.getName(), new Counts(0, 1, 0, 3));\n        counts.put(Citizen.class.getName() + \"##NaturalId\", new Counts(0, 1, 0, 3));\n        verifyFindCitizenByNaturalId(\"78902007R\", \"Stark\", counts);\n    }\n\n    private void verifyFindCountryByNaturalId(String callingCode, String expectedName) {\n        clearStatistics();\n\n        QuarkusTransaction.requiringNew().run(() -> {\n            final Session session = em.unwrap(Session.class);\n            final NaturalIdLoadAccess<Country> loader = session.byNaturalId(Country.class);\n            loader.using(\"callingCode\", callingCode);\n            Country country = loader.load();\n            if (!country.getName().equals(expectedName))\n                throw new RuntimeException(\"Incorrect citizen: \" + country.getName() + \", expected: \" + expectedName);\n        });\n    }\n\n    private void updateNaturalId(Map<String, Counts> counts) {\n        clearStatistics();\n\n        QuarkusTransaction.requiringNew().run(() -> {\n            final Session session = em.unwrap(Session.class);\n            final NaturalIdLoadAccess<Citizen> loader = session.byNaturalId(Citizen.class);\n            loader.using(\"ssn\", \"45989213T\");\n            Citizen citizen = loader.load();\n            String expected = \"Stark\";\n            if (!citizen.getLastname().equals(expected))\n                throw new RuntimeException(\"Incorrect citizen: \" + citizen.getLastname() + \", expected: \" + expected);\n\n            citizen.setSsn(\"78902007R\");\n        });\n","sourceCodeStart":169,"sourceCodeEnd":205,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/integration-tests/hibernate-orm-cache/src/main/java/io/quarkus/it/hibernate/orm/cache/HibernateOrmCacheTestEndpoint.java#L169-L205","documentation":"This is a test-endpoint assertion failure inside verifyFindCountryByNaturalId: after loading a Country by its natural id (callingCode) via Hibernate's NaturalIdLoadAccess, the endpoint checks the loaded entity's name matches the expected value and throws RuntimeException if not. It indicates the second-level/natural-id cache returned the wrong (stale or incorrect) entity for that natural id.","triggerScenarios":"Calling the testNaturalId (read-only) endpoint when the cache returns a Country whose name differs from the expected mapping of callingCode -> name — e.g. stale cached data or wrong entity stored under the natural id key.","commonSituations":"Stale second-level cache after data changes; cache not invalidated between test runs; different dataset seeded than the test expects; natural id cache region misconfigured (wrong concurrency strategy) leading to inconsistent reads.","solutions":["Clear/invalidated the second-level cache regions before the test run (endpoint has clearStatistics; also evict entity cache)","Verify the seeded Country data matches the expected callingCode/name mapping","Run with a clean cache backend (fresh Caffeine/in-memory cache) to rule out stale state","Confirm @Cache concurrency strategy (READ_ONLY) is not being violated by updates"],"exampleFix":"// before\nCountry country = loader.load();\nif (!country.getName().equals(expectedName)) throw ...\n// after\nsessionFactory.getCache().evict(Country.class); // clear stale region before load\nCountry country = loader.load();\nif (!country.getName().equals(expectedName)) throw ...","handlingStrategy":"validation","validationCode":"Country country = loader.load();\nObjects.requireNonNull(country, \"No Country for callingCode \" + callingCode);\nif (!country.getName().equals(expectedName)) {\n    sessionFactory.getCache().evict(Country.class);\n    country = loader.load(); // reload from DB\n    if (!country.getName().equals(expectedName)) throw new RuntimeException(\"Data mismatch, not cache\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    verifyFindCountryByNaturalId(callingCode, expectedName);\n} catch (RuntimeException e) {\n    sessionFactory.getCache().evict(Country.class);\n    // retry once with a clean cache to distinguish staleness from bad data\n}","preventionTips":["Reset/re-seed data between test runs","Evict entity regions before cache-behavior assertions","Keep seed data and expected values in one shared constant"],"tags":["hibernate","natural-id","second-level-cache","test-assertion","quarkus"],"backgroundTag":"stale-cache-data","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"}