hibernate/hibernate-orm · error · FetchNotFoundException
Entity `%s` with identifier value `%s` does not exist
Error message
Entity `%s` with identifier value `%s` does not exist
What it means
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.
Source
Thrown at hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/internal/EntityInitializerImpl.java:917
data.entityInstanceForNotify = null;
data.entityHolder = null;
data.setState( State.MISSING );
// super processes the foreign-key target column. here we
// need to also look at the foreign-key value column to check
// for a dangling foreign-key
if ( keyAssembler != null ) {
final Object foreignKeyValue = keyAssembler.assemble( data.getRowProcessingState() );
if ( foreignKeyValue != null ) {
if ( notFoundAction != NotFoundAction.IGNORE ) {
final String entityName = getEntityDescriptor().getEntityName();
if ( affectedByFilter ) {
throw new EntityFilterException( entityName, foreignKeyValue,
referencedModelPart.getNavigableRole().getFullPath() );
}
else {
throw new FetchNotFoundException( entityName, foreignKeyValue );
}
}
}
}
}
@Override
public void resolveFromPreviousRow(EntityInitializerData data) {
if ( data.getState() == State.UNINITIALIZED ) {
final var entityKey = data.entityKey;
if ( entityKey == null ) {
setMissing( data );
}
else {
data.setState( State.INITIALIZED );
notifySubInitializersToReusePreviousRowInstance( data );
}
}View on GitHub (pinned to fad1729dce)
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.
Example fix
-- find dangling references SELECT 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; // before @ManyToOne(optional = false, fetch = FetchType.EAGER) @JoinColumn(name = "parent_id") private Parent parent; // after @ManyToOne @JoinColumn(name = "parent_id") @NotFound(action = NotFoundAction.IGNORE) private Parent parent;
Defensive patterns
Strategy: validation
Validate before calling
// Integrity pre-check for every mandatory FK the query traverses
String sql = "select c.id, c.parent_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";
if (!em.createNativeQuery(sql).getResultList().isEmpty()) {
throw new IllegalStateException("dangling parent_id references found");
} Try / catch
try {
List<Child> rows = em.createQuery("select c from Child c join fetch c.parent", Child.class).getResultList();
} catch (FetchNotFoundException e) {
// log entity name + identifier, quarantine the row, continue with the rest
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Entity `%s` with identifier value `%s` does not exist
- Entity `%s` with identifier value `%s` does not exist
- Entity `%s` with identifier value `%s` does not exist
- Cannot update previous revision for entity %s and id %s (%s
- Retrieved key was null, but to-one is not nullable : %s
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/a99f5ac14e950fde.
Report an issue: GitHub.