{"record":{"id":"06bdb8ddff9dc800","repo":"hibernate/hibernate-orm","slug":"unknown-service-requested-servicerole-getname","errorCode":null,"errorMessage":"Unknown service requested [${serviceRole.getName()}]","messagePattern":"Unknown service requested \\[(.+?)\\]","errorType":"exception","errorClass":"NullServiceException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/service/ServiceRegistry.java","lineNumber":69,"sourceCode":"\t/**\n\t * Retrieve a service by role, throwing an exception if there is no such service.\n\t * If service is not found, but a {@link org.hibernate.service.spi.ServiceInitiator}\n\t * is registered for this service role, the service will be initialized and returned.\n\t *\n\t * @apiNote We cannot return {@code <R extends Service<T>>} here because the service might come from the parent.\n\t *\n\t * @param serviceRole The service role\n\t * @param <R> The service role type\n\t *\n\t * @return The requested service .\n\t *\n\t * @throws UnknownServiceException Indicates the service was not known.\n\t * @throws NullServiceException Indicates the service was null.\n\t */\n\tdefault <R extends Service> R requireService(@Nonnull Class<R> serviceRole) {\n\t\tfinal R service = getService( serviceRole );\n\t\tif ( service == null ) {\n\t\t\tthrow new NullServiceException( serviceRole );\n\t\t}\n\t\treturn service;\n\t}\n\n\t@Override\n\tvoid close();\n}\n","sourceCodeStart":51,"sourceCodeEnd":77,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/service/ServiceRegistry.java#L51-L77","documentation":"ServiceRegistry.requireService(Class) delegates to getService(role); when the registry has no binding for that role, getService returns null and requireService converts it into NullServiceException, whose message is \"Unknown service requested [<role>]\". It means the requested service role simply is not registered in this registry instance.","triggerScenarios":"Calling requireService for a role that was never registered via a ServiceInitiator, ServiceContributor, or builder.addService(); asking the wrong registry level (Bootstrap vs Standard vs SessionFactory-owned) for a role owned by another; requesting a service after the registry was stopped.","commonSituations":"Custom service roles not contributed through META-INF/services ServiceContributor; typos or wrong class passed as role; assuming an optional Hibernate service always exists; accessing SessionFactory-level services before the factory is built.","solutions":["Use getService(role) with an explicit null check when the service is optional","Register the role: implement a StandardServiceInitiator/ServiceContributor or call StandardServiceRegistryBuilder.addService()","Request the service from the registry that owns it (e.g. via SessionFactoryImplementor.getServiceRegistry())","Check the registry is still open — a stopped registry no longer resolves services"],"exampleFix":"// before\nfinal Dialect dialect = serviceRegistry.requireService(Dialect.class); // not bound at this level\n\n// after\nDialect dialect = serviceRegistry.getService(Dialect.class);\nif (dialect == null) {\n    // fall back to the registry that owns the service, or register it\n    dialect = sessionFactory.getServiceRegistry().requireService(JdbcServices.class).getDialect();\n}","handlingStrategy":"validation","validationCode":"ConnectionProvider cp = serviceRegistry.getService(ConnectionProvider.class);\nif (cp == null) {\n    // optional path: register it or fail with a precise message\n    throw new IllegalStateException(\"ConnectionProvider not registered in this registry\");\n}","typeGuard":"boolean isServiceAvailable(ServiceRegistry registry, Class<? extends Service> role) {\n    return registry.getService(role) != null;\n}","tryCatchPattern":"try {\n    return registry.requireService(role);\n}\ncatch (NullServiceException e) {\n    // role not registered in this registry; register it or request from the owning registry\n    LOG.warn(\"service {} unknown to this registry\", role.getName());\n    return fallbackRegistry().requireService(role);\n}","preventionTips":["Use getService() + null check for optional services; reserve requireService() for mandatory ones","Register custom roles via ServiceContributor discovered through META-INF/services","Request services from the registry that owns them (SessionFactory-level vs Standard vs Bootstrap)"],"tags":["service-registry","bootstrap","configuration"],"backgroundTag":"unknown-service-role","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}