quarkusio/quarkus · error · EntityNotFoundException
Unable to find <entityName> with id <id>
Error message
Unable to find <entityName> with id <id>
What it means
Quarkus's fast-boot EMF installs a JpaEntityNotFoundDelegate that converts Hibernate's internal 'entity not found' condition into javax.persistence.EntityNotFoundException with a uniform message. It is thrown lazily when a proxy or reference is accessed but the row does not exist in the database.
Source
Thrown at extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/boot/FastBootEntityManagerFactoryBuilder.java:255
@Override
public void sessionFactoryCreated(SessionFactory sessionFactory) {
// nothing to do
}
@Override
public void sessionFactoryClosed(SessionFactory sessionFactory) {
SessionFactoryImplementor sfi = ((SessionFactoryImplementor) sessionFactory);
sfi.getServiceRegistry().destroy();
ServiceRegistry basicRegistry = sfi.getServiceRegistry().getParentServiceRegistry();
((ServiceRegistryImplementor) basicRegistry).destroy();
}
}
private static class JpaEntityNotFoundDelegate implements EntityNotFoundDelegate, Serializable {
public void handleEntityNotFound(String entityName, Object id) {
throw new EntityNotFoundException("Unable to find " + entityName + " with id " + id);
}
}
@Override
public ManagedResources getManagedResources() {
throw new IllegalStateException("This method is not available at runtime in Quarkus");
}
@Override
public MetadataImplementor metadata() {
return metadata;
}
}
View on GitHub (pinned to e1c734241f)
Solutions
- Use EntityManager.find() instead of getReference() when you need to handle absence gracefully (returns null instead of throwing on proxy access).
- Check that the entity with the given id actually exists in the DB; verify your schema/import scripts ran (quarkus.hibernate-orm.sql-load-script).
- Catch EntityNotFoundException around proxy access or force initialization (Hibernate.initialize) inside the transaction.
- Validate IDs before use; fix orphaned FK data or enable foreign key constraints.
Example fix
// before
MyEntity e = em.getReference(MyEntity.class, id);
String name = e.getName(); // throws EntityNotFoundException if missing
// after
MyEntity e = em.find(MyEntity.class, id);
if (e == null) { throw new WebApplicationException(404); } Defensive patterns
Strategy: try-catch
Validate before calling
// Existence check before proxy access boolean exists = em.find(MyEntity.class, id) != null;
Try / catch
try {
MyEntity e = em.getReference(MyEntity.class, id);
use(e);
} catch (EntityNotFoundException e) {
throw new WebApplicationException(Response.status(404).build());
} Prevention
- Prefer em.find() over em.getReference() unless lazy-proxy semantics are intended
- Initialize lazy references within the transaction (Hibernate.initialize)
- Verify sql-load-script / seed data in tests so referenced IDs exist
- Enforce FK constraints so stale references surface at write time
When it happens
Trigger: Calling EntityManager.getReference(MyEntity.class, id) and then accessing any method on the returned proxy when the row was deleted or the id is wrong; lazy association resolved to a row that no longer exists (missing FK, stale cache); load() returning a proxy whose target is absent.
Common situations: Deleted rows referenced by stale foreign keys; tests using hard-coded IDs; missing database initialization (import.sql not loaded) so referenced data is absent; read of an uninitialized lazy proxy after the underlying record was removed in another transaction.
Understand the failure class
Background: "Not found" and "does not exist" errors: why "Task not found", "No such folder", and "Can't find" fire when a lookup comes back empty — this error's family across 14 libraries.
Related errors
- Methods that are annotated with JPA Listener annotations sho
- @PersistenceUnit annotations are not supported at the class
- This PersistenceProvider does not support createEntityManage
- Unable to find an EntityManagerFactory for persistence unit
- Persistence unit is closed
AI-assisted analysis of quarkusio/quarkus@e1c734241f (2026-09-05).
Data as JSON: /api/errors/2d8500eeff54ed70.
Report an issue: GitHub.