{"record":{"id":"17be45c9a991642e","repo":"hibernate/hibernate-orm","slug":"typeconfiguration-is-not-currently-scoped-to-metad","errorCode":null,"errorMessage":"TypeConfiguration is not currently scoped to MetadataBuildingContext","messagePattern":"TypeConfiguration is not currently scoped to MetadataBuildingContext","errorType":"exception","errorClass":"HibernateException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/type/spi/TypeConfiguration.java","lineNumber":524,"sourceCode":"\t\tpublic ClassLoaderService getClassLoaderService() {\n\t\t\treturn sessionFactory == null\n\t\t\t\t\t? metadataBuildingContext.getBootstrapContext().getClassLoaderService()\n\t\t\t\t\t: sessionFactory.getClassLoaderService();\n\t\t}\n\n\t\tpublic ManagedBeanRegistry getManagedBeanRegistry() {\n\t\t\treturn sessionFactory == null\n\t\t\t\t\t? metadataBuildingContext.getBootstrapContext().getManagedBeanRegistry()\n\t\t\t\t\t: sessionFactory.getManagedBeanRegistry();\n\t\t}\n\n\t\tprivate Scope(TypeConfiguration typeConfiguration) {\n\t\t\tthis.typeConfiguration = typeConfiguration;\n\t\t}\n\n\t\tprivate MetadataBuildingContext getMetadataBuildingContext() {\n\t\t\tif ( metadataBuildingContext == null ) {\n\t\t\t\tthrow new HibernateException( \"TypeConfiguration is not currently scoped to MetadataBuildingContext\" );\n\t\t\t}\n\t\t\treturn metadataBuildingContext;\n\t\t}\n\n\t\tprivate ServiceRegistry getServiceRegistry() {\n\t\t\tif ( metadataBuildingContext != null ) {\n\t\t\t\treturn metadataBuildingContext.getBootstrapContext().getServiceRegistry();\n\t\t\t}\n\t\t\telse if ( sessionFactory != null ) {\n\t\t\t\treturn sessionFactory.getServiceRegistry();\n\t\t\t}\n\t\t\telse {\n\t\t\t\tthrow new AssertionFailure( \"No service registry available\" );\n\t\t\t}\n\t\t}\n\n\t\tprivate JpaCompliance getJpaCompliance() {\n\t\t\tif ( metadataBuildingContext != null ) {","sourceCodeStart":506,"sourceCodeEnd":542,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/type/spi/TypeConfiguration.java#L506-L542","documentation":"A TypeConfiguration is scoped first to a MetadataBuildingContext during boot, then to the SessionFactory at runtime, and both references are released when the factory closes (see the lifecycle javadoc at TypeConfiguration.java:379-399). Scope.getMetadataBuildingContext (TypeConfiguration.java:520-526) throws this HibernateException when the boot-time context is already gone - either because the SessionFactory has been built (scope moved to runtime) or because the whole scope was released.","triggerScenarios":"Calling typeConfiguration.getMetadataBuildingContext() (or a public API that routes to it, e.g. TypeConfiguration.java:188-190, 214) after the SessionFactory was created; caching a TypeConfiguration obtained during bootstrap and using boot-time APIs on it at runtime; using a stale TypeConfiguration after sessionFactory.close() released the scope.","commonSituations":"Framework/integration code that stores MetadataBuildingContext-derived objects and reuses them per request; Envers/Search-like integrations initializing lazily after boot; unit tests that rebuild factories while holding references from the previous boot; library code assuming boot context survives factory creation.","solutions":["Move the boot-time work before SessionFactory creation: do it inside the bootstrap callback / MetadataBuilder / MetadataBuildingContext scope while it is valid.","At runtime, obtain services from the SessionFactory or its ServiceRegistry (sessionFactory.getServiceRegistry()) instead of the MetadataBuildingContext.","Stop caching TypeConfiguration/Type objects across lifecycle boundaries - re-resolve them from the current factory when needed.","If a stale reference after close() is the cause, rebuild or re-acquire the objects after the new factory is created."],"exampleFix":"// before - boot-time API used after the factory exists\nTypeConfiguration tc = cachedBootTypeConfiguration;\nMetadataBuildingContext ctx = tc.getMetadataBuildingContext(); // throws\n\n// after - use runtime scope\nServiceRegistry registry = sessionFactory.getServiceRegistry();\n// or do the work during boot, before SessionFactory build:\nMetadata metadata = metadataSources.buildMetadata(); // boot context valid here","handlingStrategy":"validation","validationCode":"// Check the lifecycle stage before using boot-time APIs on a TypeConfiguration\npublic static void safeBootOperation(TypeConfiguration tc, Runnable op) {\n    try {\n        tc.getMetadataBuildingContext(); // still boot-scoped?\n        op.run();\n    } catch ( HibernateException e ) {\n        throw new IllegalStateException(\n            \"Boot context gone - use sessionFactory.getServiceRegistry() at runtime\", e );\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    metadataBuildingContextApi( typeConfiguration );\n} catch ( org.hibernate.HibernateException e ) {\n    if ( e.getMessage() != null && e.getMessage().contains( \"not currently scoped\" ) ) {\n        // scope moved to SessionFactory or was released - re-acquire from the factory\n    } else throw e;\n}","preventionTips":["Never cache TypeConfiguration, MetadataBuildingContext or boot Types past SessionFactory creation.","At runtime, resolve types via sessionFactory.getTypeConfiguration() / getServiceRegistry(), not boot objects.","Design integrations to do all boot-time work during bootstrap callbacks (MetadataContributor, TypeContributor).","In tests, rebuild references whenever the SessionFactory is rebuilt.","Close factories deterministically and null out shared references afterwards."],"tags":["hibernate","lifecycle","session-factory","bootstrap","type-configuration"],"backgroundTag":"orm-bootstrapping-lifecycle","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}