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
EntityDelayedFetchInitializer handles to-one associations resolved 'delayed' (the FK id is read now, the association resolved later from the persistence context or by id). When the row for the target is checked, a null discriminator means no row exists in the target table for that id; if the association is not optional (referencedModelPart.isOptional() == false), Hibernate throws FetchNotFoundException('Entity X with identifier value Y does not exist'). This is the signature of a dangling foreign key: a non-null FK column pointing at an entity that is gone.
Source
Thrown at hibernate-core/src/main/java/org/hibernate/sql/results/graph/entity/internal/EntityDelayedFetchInitializer.java:162
data.entityIdentifier = identifierAssembler.assemble( rowProcessingState );
if ( data.entityIdentifier == null ) {
data.setInstance( null );
data.setState( State.MISSING );
}
else {
final var entityPersister = getEntityDescriptor();
final EntityPersister concreteDescriptor;
if ( discriminatorAssembler != null ) {
concreteDescriptor = determineConcreteEntityDescriptor(
rowProcessingState,
discriminatorAssembler,
entityPersister
);
if ( concreteDescriptor == null ) {
// If we find no discriminator, it means there's no entity in the target table
if ( !referencedModelPart.isOptional() ) {
throw new FetchNotFoundException( entityPersister.getEntityName(), data.entityIdentifier );
}
data.setInstance( null );
data.setState( State.MISSING );
return;
}
}
else {
concreteDescriptor = entityPersister;
}
initialize( data, null, concreteDescriptor );
}
}
}
protected void initialize(
EntityDelayedFetchInitializerData data,
@Nullable EntityKey entityKey,View on GitHub (pinned to fad1729dce)
Solutions
- Repair the data: either restore the missing referenced rows or NULL out / delete the orphaned FK values.
- Add and enforce a real FOREIGN KEY constraint so the database rejects dangling references in the first place.
- If absence is legitimate, map the association nullable (@ManyToOne(optional=true) with a nullable FK column) or annotate @NotFound(action = NotFoundAction.IGNORE) so Hibernate nulls the association instead of throwing.
- Investigate whether @Filter/@SQLRestriction on the target entity hides rows that actually exist.
Example fix
// before @ManyToOne(optional = false) // FK points to a deleted Client row private Client client; -- fix data + prevent recurrence UPDATE invoice SET client_id = NULL WHERE client_id NOT IN (SELECT id FROM client); ALTER TABLE invoice ADD CONSTRAINT fk_invoice_client FOREIGN KEY (client_id) REFERENCES client (id); // after (if a missing target is a legal state) @ManyToOne @NotFound(action = NotFoundAction.IGNORE) private Client client;
Defensive patterns
Strategy: try-catch
Validate before calling
// Pre-check for dangling mandatory FKs before running the business query
String sql = "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";
List<?> orphans = em.createNativeQuery(sql).getResultList();
if (!orphans.isEmpty()) throw new IllegalStateException("Dangling FKs: " + orphans); Try / catch
try {
Order o = em.createQuery("select o from Order o join fetch o.client", Order.class).getSingleResult();
} catch (FetchNotFoundException e) {
// e.getEntityName() / e.getIdentifier() tell you which reference is dangling
// handle: alert data team, skip the record, or treat association as absent
} Prevention
- Declare and enforce database FOREIGN KEY constraints for every to-one association.
- Run a periodic anti-join integrity job for tables whose FKs cannot be constrained.
- Decide explicitly per association: mandatory (optional=false) or tolerant (@NotFound(IGNORE)); never rely on luck.
- Keep deletes and inserts in one transaction so orphans cannot be committed.
When it happens
Trigger: A mandatory (optional=false / non-null @ManyToOne or @OneToOne) association whose FK column contains an id with no matching row in the target table; rows deleted while referencing rows kept (FK constraints disabled or absent); target row hidden by @SQLRestriction/@Filter so the discriminator reads as null.
Common situations: Manual deletes or bulk cleanup scripts that orphan FKs; databases with FK checking disabled; legacy schemas without FK constraints; environments where another service deletes reference data still referenced by your tables.
Related errors
- Retrieved key was null, but to-one is not nullable : %s
- Entity `%s` with identifier value `%s` does not exist
- Entity `%s` with identifier value `%s` does not exist
- Referenced entity '" + referencedEntityName + "' does not ex
- Referenced entity '" + referencedEntityName + "' has no prop
AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22).
Data as JSON: /api/errors/866ff52b5171e9ac.
Report an issue: GitHub.