{"record":{"id":"64d4aad1daa36686","repo":"hibernate/hibernate-orm","slug":"entity-s-with-identifier-value-s-does-not-ex-64d4aa","errorCode":null,"errorMessage":"Entity `%s` with identifier value `%s` does not exist","messagePattern":"Entity `(.+?)` with identifier value `(.+?)` does not exist","errorType":"exception","errorClass":"FetchNotFoundException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/internal/JoinedDiscriminatedEntityInitializer.java","lineNumber":218,"sourceCode":"\t\tfinal Object foreignKeyValue = keyValueAssembler.assemble( data.getRowProcessingState() );\n\t\tif ( foreignKeyValue != null ) {\n\t\t\tfinal var concreteInitializer = concreteInitializersByEntityName.get( entityName );\n\t\t\tfinal boolean filteredOut;\n\t\t\tif ( concreteInitializer == null ) {\n\t\t\t\t// Discriminator is null, but foreign key is given. Let's just assume this was filtered out,\n\t\t\t\t// if any initializer was affected by a filter\n\t\t\t\tfilteredOut = !affectedByFilter.isEmpty();\n\t\t\t}\n\t\t\telse {\n\t\t\t\tfinal var index = ArrayHelper.indexOf( concreteInitializers, concreteInitializer );\n\t\t\t\tassert index >= 0;\n\t\t\t\tfilteredOut = affectedByFilter.get( index );\n\t\t\t}\n\t\t\tif ( filteredOut ) {\n\t\t\t\tthrow new EntityFilterException( entityName, foreignKeyValue,\n\t\t\t\t\t\tfetchedPart.getNavigableRole().getFullPath() );\n\t\t\t}\n\t\t\tthrow new FetchNotFoundException( entityName, foreignKeyValue );\n\t\t}\n\t}\n\n\t@Override\n\tpublic void resolveFromPreviousRow(JoinedDiscriminatedEntityInitializerData data) {\n\t\tif ( data.getState() == State.UNINITIALIZED ) {\n\t\t\tif ( data.getInstance() == null ) {\n\t\t\t\tdata.setState( State.MISSING );\n\t\t\t}\n\t\t\telse {\n\t\t\t\tfinal var initializer = keyValueAssembler.getInitializer();\n\t\t\t\tif ( initializer != null ) {\n\t\t\t\t\tinitializer.resolveFromPreviousRow( data.getRowProcessingState() );\n\t\t\t\t}\n\t\t\t\tif ( data.concreteInitializer != null ) {\n\t\t\t\t\tdata.concreteInitializer.resolveFromPreviousRow( data.getRowProcessingState() );\n\t\t\t\t}\n\t\t\t\tdata.setState( State.INITIALIZED );","sourceCodeStart":200,"sourceCodeEnd":236,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/internal/JoinedDiscriminatedEntityInitializer.java#L200-L236","documentation":"The not-filtered fallback in JoinedDiscriminatedEntityInitializer: after resolving a to-one association to a JOINED-inheritance target via join, the FK is non-null but no concrete subclass row was found, no filter explains it, and NotFoundAction is EXCEPTION - so FetchNotFoundException('Entity X with identifier value Y does not exist') is thrown. The FK references a hierarchy instance whose row(s) are absent from the joined result.","triggerScenarios":"A non-null FK to a JOINED inheritance entity whose base or subclass row is missing; deletes that removed the subclass row but left the base row (or vice versa); discriminator present but the corresponding subclass table row absent; no FK constraint to prevent the orphan.","commonSituations":"Partially deleted JOINED-inheritance data (base row without subclass row or the reverse); manual data surgery on inheritance tables; inconsistent restores; imports that wrote base rows but skipped subclass rows.","solutions":["Find and repair inconsistent hierarchy rows: FKs pointing at ids with no base row at all, and base rows without their subclass row.","Add FOREIGN KEY constraints for both the base table PK relationships and each subclass table's FK to the base, so partial deletes are rejected.","If absence is legal, use @NotFound(action = NotFoundAction.IGNORE) or optional=true so the association resolves to null.","Re-check whether a filter was recently added - if so move to the EntityFilterException remedy, since that is the actual cause."],"exampleFix":"-- find FKs with no base-table row\nSELECT o.id, o.doc_id FROM \"order\" o LEFT JOIN document d ON d.id = o.doc_id\nWHERE o.doc_id IS NOT NULL AND d.id IS NULL;\n\n-- find JOINED hierarchies missing their subclass row\nSELECT d.id FROM document d LEFT JOIN contract c ON c.id = d.id\nWHERE d.dtype = 'CONTRACT' AND c.id IS NULL;\n\n// before\n@ManyToOne(optional = false)\nprivate Document document;\n\n// after\n@ManyToOne\n@NotFound(action = NotFoundAction.IGNORE)\nprivate Document document;","handlingStrategy":"try-catch","validationCode":"// Check JOINED hierarchy consistency + dangling FKs before loading\nString base = \"select o.id from \\\"order\\\" o left join document d on d.id = o.doc_id \"\n            + \"where o.doc_id is not null and d.id is null\";\nString sub  = \"select d.id from document d left join contract c on c.id = d.id \"\n            + \"where d.dtype = 'CONTRACT' and c.id is null\";\nif (!em.createNativeQuery(base).getResultList().isEmpty()\n        || !em.createNativeQuery(sub).getResultList().isEmpty()) {\n    throw new IllegalStateException(\"inconsistent JOINED inheritance data\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    List<Order> l = em.createQuery(\"select o from Order o join fetch o.document\", Order.class).getResultList();\n} catch (FetchNotFoundException e) {\n    // e.getIdentifier() -> the doc id with no base/subclass row; repair or ignore\n}","preventionTips":["Add FK constraints from each subclass table to the base table PK.","Delete JOINED hierarchy rows base-plus-subclass in one transaction only.","Add periodic consistency queries (base without subclass, FK without base) to ops checks."],"tags":["hibernate","orm","inheritance","joined-inheritance","dangling-foreign-key","fetch-not-found","data-integrity"],"backgroundTag":"dangling-foreign-key","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}