{"record":{"id":"0ef08fe4658e9b40","repo":"hibernate/hibernate-orm","slug":"entity-s-with-identifier-value-s-does-not-ex-0ef08f","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/EntitySelectFetchInitializer.java","lineNumber":306,"sourceCode":"\n\tvoid checkNotFound(EntitySelectFetchInitializerData data) {\n\t\tcheckNotFound( toOneMapping, affectedByFilter,\n\t\t\t\tconcreteDescriptor.getEntityName(),\n\t\t\t\tdata.entityIdentifier );\n\t}\n\n\tstatic void checkNotFound(\n\t\t\tToOneAttributeMapping toOneMapping,\n\t\t\tboolean affectedByFilter,\n\t\t\tString entityName, Object identifier) {\n\t\tfinal var notFoundAction = toOneMapping.getNotFoundAction();\n\t\tif ( notFoundAction != NotFoundAction.IGNORE ) {\n\t\t\tif ( affectedByFilter ) {\n\t\t\t\tthrow new EntityFilterException( entityName, identifier,\n\t\t\t\t\t\ttoOneMapping.getNavigableRole().getFullPath() );\n\t\t\t}\n\t\t\tif ( notFoundAction == NotFoundAction.EXCEPTION ) {\n\t\t\t\tthrow new FetchNotFoundException( entityName, identifier );\n\t\t\t}\n\t\t}\n\t}\n\n\t@Override\n\tpublic void initializeInstanceFromParent(Object parentInstance, Data data) {\n\t\tfinal var attributeMapping = getInitializedPart().asAttributeMapping();\n\t\tfinal Object instance =\n\t\t\t\tattributeMapping != null\n\t\t\t\t\t\t? attributeMapping.getValue( parentInstance )\n\t\t\t\t\t\t: parentInstance;\n\t\tif ( instance == null ) {\n\t\t\tdata.setState( State.MISSING );\n\t\t\tdata.entityIdentifier = null;\n\t\t\tdata.setInstance( null );\n\t\t}\n\t\telse {\n\t\t\tdata.setState( State.INITIALIZED );","sourceCodeStart":288,"sourceCodeEnd":324,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/internal/EntitySelectFetchInitializer.java#L288-L324","documentation":"The not-filtered branch of EntitySelectFetchInitializer.checkNotFound(): the secondary SELECT for a select-fetched to-one association found no row for the FK's id, notFoundAction == EXCEPTION (default), so FetchNotFoundException('Entity X with identifier value Y does not exist') is thrown. Same dangling-foreign-key semantics as the joined variant, but detected during the extra select performed to resolve the association.","triggerScenarios":"A lazy/select-fetched @ManyToOne/@OneToOne with a non-null FK whose target row is missing; target deleted between the main query and the association select (or simply absent); database lacks FK constraints so the orphan was never prevented.","commonSituations":"Reference data deleted by cleanup jobs while other tables still point at it; test fixtures that insert FKs without parents; cross-service databases where another service deletes rows your schema references.","solutions":["Repair the orphaned FKs (anti-join query to find them, then NULL or delete) and add a FOREIGN KEY constraint.","If absence is acceptable, use @NotFound(action = NotFoundAction.IGNORE) or optional=true with a nullable column.","Switch the association to eagerly joined and validate at load time, so problems surface with full SQL context.","Point the FK at an existing row (e.g. a default/unknown placeholder entity) in systems where the reference is mandatory."],"exampleFix":"-- find orphans before they explode at runtime\nSELECT c.id, c.product_id FROM cart_item c LEFT JOIN product p ON p.id = c.product_id\nWHERE c.product_id IS NOT NULL AND p.id IS NULL;\n\n// before\n@ManyToOne(fetch = FetchType.LAZY)\nprivate Product product;\n\n// after\n@ManyToOne(fetch = FetchType.LAZY)\n@NotFound(action = NotFoundAction.IGNORE)\nprivate Product product;","handlingStrategy":"try-catch","validationCode":"// Pre-check the FK targets the lazy association will select\nString sql = \"select ci.id from cart_item ci left join product p on p.id = ci.product_id \"\n           + \"where ci.product_id is not null and p.id is null\";\nif (!em.createNativeQuery(sql).getResultList().isEmpty()) {\n    throw new IllegalStateException(\"cart_item rows reference missing products\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    cart.getProducts().size(); // triggers the secondary select\n} catch (FetchNotFoundException e) {\n    // e.getIdentifier() is the dangling product id: quarantine or repair the row\n}","preventionTips":["Add FK constraints so orphaned lazy references cannot be written.","Prefer @NotFound(action = IGNORE) for references to volatile or deletable data.","Run integrity checks after bulk deletes and cross-service cleanups."],"tags":["hibernate","orm","lazy-loading","dangling-foreign-key","fetch-not-found","select-fetch"],"backgroundTag":"dangling-foreign-key","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}