{"record":{"id":"a99f5ac14e950fde","repo":"hibernate/hibernate-orm","slug":"entity-s-with-identifier-value-s-does-not-ex-a99f5a","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/EntityInitializerImpl.java","lineNumber":917,"sourceCode":"\t\tdata.entityInstanceForNotify = null;\n\t\tdata.entityHolder = null;\n\t\tdata.setState( State.MISSING );\n\n\t\t// super processes the foreign-key target column.  here we\n\t\t// need to also look at the foreign-key value column to check\n\t\t// for a dangling foreign-key\n\n\t\tif ( keyAssembler != null ) {\n\t\t\tfinal Object foreignKeyValue = keyAssembler.assemble( data.getRowProcessingState() );\n\t\t\tif ( foreignKeyValue != null ) {\n\t\t\t\tif ( notFoundAction != NotFoundAction.IGNORE ) {\n\t\t\t\t\tfinal String entityName = getEntityDescriptor().getEntityName();\n\t\t\t\t\tif ( affectedByFilter ) {\n\t\t\t\t\t\tthrow new EntityFilterException( entityName, foreignKeyValue,\n\t\t\t\t\t\t\t\treferencedModelPart.getNavigableRole().getFullPath() );\n\t\t\t\t\t}\n\t\t\t\t\telse {\n\t\t\t\t\t\tthrow new FetchNotFoundException( entityName, foreignKeyValue );\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t}\n\n\t@Override\n\tpublic void resolveFromPreviousRow(EntityInitializerData data) {\n\t\tif ( data.getState() == State.UNINITIALIZED ) {\n\t\t\tfinal var entityKey = data.entityKey;\n\t\t\tif ( entityKey == null ) {\n\t\t\t\tsetMissing( data );\n\t\t\t}\n\t\t\telse {\n\t\t\t\tdata.setState( State.INITIALIZED );\n\t\t\t\tnotifySubInitializersToReusePreviousRowInstance( data );\n\t\t\t}\n\t\t}","sourceCodeStart":899,"sourceCodeEnd":935,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/internal/EntityInitializerImpl.java#L899-L935","documentation":"The sibling of EntityFilterException in the same setMissing() block: EntityInitializerImpl found a non-null FK value for a joined to-one fetch but no target row, no filter explains the absence, and notFoundAction != IGNORE - so it throws FetchNotFoundException('Entity X with identifier value Y does not exist'). This is Hibernate's canonical 'the foreign key points at a row that is not there' error during result-set processing.","triggerScenarios":"Querying an entity whose non-null FK (joined @ManyToOne/@OneToOne) references an id absent from the target table; deletes performed outside Hibernate (native SQL, another service, DBA scripts) after the referencing rows were written; partially committed imports; legacy data with FK constraints never enforced.","commonSituations":"Reference-data cleanup that orphans rows; test databases seeded inconsistently; production restores where child tables are newer than parent tables; schemas without FOREIGN KEY constraints.","solutions":["Restore the missing rows or null/delete the orphaned FK values (find them with an anti-join, see validation below).","Add a database FOREIGN KEY constraint to make dangling FKs impossible going forward.","If the target can legitimately disappear, map @ManyToOne(optional=true) with a nullable column, or @NotFound(action = NotFoundAction.IGNORE).","Re-run the failing query after repair to confirm the anti-join returns zero rows."],"exampleFix":"-- find dangling references\nSELECT c.id FROM child c LEFT JOIN parent p ON p.id = c.parent_id WHERE c.parent_id IS NOT NULL AND p.id IS NULL;\n\n// before\n@ManyToOne(optional = false, fetch = FetchType.EAGER)\n@JoinColumn(name = \"parent_id\")\nprivate Parent parent;\n\n// after\n@ManyToOne\n@JoinColumn(name = \"parent_id\")\n@NotFound(action = NotFoundAction.IGNORE)\nprivate Parent parent;","handlingStrategy":"validation","validationCode":"// Integrity pre-check for every mandatory FK the query traverses\nString sql = \"select c.id, c.parent_id from child c left join parent p on p.id = c.parent_id \"\n           + \"where c.parent_id is not null and p.id is null\";\nif (!em.createNativeQuery(sql).getResultList().isEmpty()) {\n    throw new IllegalStateException(\"dangling parent_id references found\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    List<Child> rows = em.createQuery(\"select c from Child c join fetch c.parent\", Child.class).getResultList();\n} catch (FetchNotFoundException e) {\n    // log entity name + identifier, quarantine the row, continue with the rest\n}","preventionTips":["Enforce FK constraints in the schema; treat their absence as a defect.","Schedule anti-join integrity checks in ops dashboards for legacy data.","Never delete reference data with raw SQL without checking dependents.","Map optional references nullable so absence degrades to null, not an exception."],"tags":["hibernate","orm","data-integrity","dangling-foreign-key","fetch-not-found","jdbc-result"],"backgroundTag":"dangling-foreign-key","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}