{"record":{"id":"2d8500eeff54ed70","repo":"quarkusio/quarkus","slug":"unable-to-find-entityname-with-id-id","errorCode":null,"errorMessage":"Unable to find <entityName> with id <id>","messagePattern":"Unable to find <entityName> with id <id>","errorType":"exception","errorClass":"EntityNotFoundException","httpStatus":null,"severity":"error","filePath":"extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/boot/FastBootEntityManagerFactoryBuilder.java","lineNumber":255,"sourceCode":"\n        @Override\n        public void sessionFactoryCreated(SessionFactory sessionFactory) {\n            // nothing to do\n        }\n\n        @Override\n        public void sessionFactoryClosed(SessionFactory sessionFactory) {\n            SessionFactoryImplementor sfi = ((SessionFactoryImplementor) sessionFactory);\n            sfi.getServiceRegistry().destroy();\n            ServiceRegistry basicRegistry = sfi.getServiceRegistry().getParentServiceRegistry();\n            ((ServiceRegistryImplementor) basicRegistry).destroy();\n        }\n    }\n\n    private static class JpaEntityNotFoundDelegate implements EntityNotFoundDelegate, Serializable {\n\n        public void handleEntityNotFound(String entityName, Object id) {\n            throw new EntityNotFoundException(\"Unable to find \" + entityName + \" with id \" + id);\n        }\n    }\n\n    @Override\n    public ManagedResources getManagedResources() {\n        throw new IllegalStateException(\"This method is not available at runtime in Quarkus\");\n    }\n\n    @Override\n    public MetadataImplementor metadata() {\n        return metadata;\n    }\n\n}\n","sourceCodeStart":237,"sourceCodeEnd":270,"githubUrl":"https://github.com/quarkusio/quarkus/blob/e1c734241f34c7919086ceb4c9262b4a58f6de44/extensions/hibernate-orm/runtime/src/main/java/io/quarkus/hibernate/orm/runtime/boot/FastBootEntityManagerFactoryBuilder.java#L237-L270","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nMyEntity e = em.getReference(MyEntity.class, id);\nString name = e.getName(); // throws EntityNotFoundException if missing\n// after\nMyEntity e = em.find(MyEntity.class, id);\nif (e == null) { throw new WebApplicationException(404); }","handlingStrategy":"try-catch","validationCode":"// Existence check before proxy access\nboolean exists = em.find(MyEntity.class, id) != null;","typeGuard":null,"tryCatchPattern":"try {\n    MyEntity e = em.getReference(MyEntity.class, id);\n    use(e);\n} catch (EntityNotFoundException e) {\n    throw new WebApplicationException(Response.status(404).build());\n}","preventionTips":["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"],"tags":["jpa","entity-not-found","lazy-loading","hibernate-orm","getreference"],"backgroundTag":"entity-not-found","analyzedSha":"e1c734241f34c7919086ceb4c9262b4a58f6de44","analyzedAt":"2026-09-05T17:01:29.979Z","contentChangedAt":"2026-09-05T17:01:29.979Z","schemaVersion":2},"datasetVersion":"2026-09-12T22:17:10.623Z"}