hibernate/hibernate-orm · error · HibernateException

TypeConfiguration is not currently scoped to MetadataBuildin

Error message

TypeConfiguration is not currently scoped to MetadataBuildingContext

What it means

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.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/type/spi/TypeConfiguration.java:524

		public ClassLoaderService getClassLoaderService() {
			return sessionFactory == null
					? metadataBuildingContext.getBootstrapContext().getClassLoaderService()
					: sessionFactory.getClassLoaderService();
		}

		public ManagedBeanRegistry getManagedBeanRegistry() {
			return sessionFactory == null
					? metadataBuildingContext.getBootstrapContext().getManagedBeanRegistry()
					: sessionFactory.getManagedBeanRegistry();
		}

		private Scope(TypeConfiguration typeConfiguration) {
			this.typeConfiguration = typeConfiguration;
		}

		private MetadataBuildingContext getMetadataBuildingContext() {
			if ( metadataBuildingContext == null ) {
				throw new HibernateException( "TypeConfiguration is not currently scoped to MetadataBuildingContext" );
			}
			return metadataBuildingContext;
		}

		private ServiceRegistry getServiceRegistry() {
			if ( metadataBuildingContext != null ) {
				return metadataBuildingContext.getBootstrapContext().getServiceRegistry();
			}
			else if ( sessionFactory != null ) {
				return sessionFactory.getServiceRegistry();
			}
			else {
				throw new AssertionFailure( "No service registry available" );
			}
		}

		private JpaCompliance getJpaCompliance() {
			if ( metadataBuildingContext != null ) {

View on GitHub (pinned to fad1729dce)

Solutions

  1. Move the boot-time work before SessionFactory creation: do it inside the bootstrap callback / MetadataBuilder / MetadataBuildingContext scope while it is valid.
  2. At runtime, obtain services from the SessionFactory or its ServiceRegistry (sessionFactory.getServiceRegistry()) instead of the MetadataBuildingContext.
  3. Stop caching TypeConfiguration/Type objects across lifecycle boundaries - re-resolve them from the current factory when needed.
  4. If a stale reference after close() is the cause, rebuild or re-acquire the objects after the new factory is created.

Example fix

// before - boot-time API used after the factory exists
TypeConfiguration tc = cachedBootTypeConfiguration;
MetadataBuildingContext ctx = tc.getMetadataBuildingContext(); // throws

// after - use runtime scope
ServiceRegistry registry = sessionFactory.getServiceRegistry();
// or do the work during boot, before SessionFactory build:
Metadata metadata = metadataSources.buildMetadata(); // boot context valid here
Defensive patterns

Strategy: validation

Validate before calling

// Check the lifecycle stage before using boot-time APIs on a TypeConfiguration
public static void safeBootOperation(TypeConfiguration tc, Runnable op) {
    try {
        tc.getMetadataBuildingContext(); // still boot-scoped?
        op.run();
    } catch ( HibernateException e ) {
        throw new IllegalStateException(
            "Boot context gone - use sessionFactory.getServiceRegistry() at runtime", e );
    }
}

Try / catch

try {
    metadataBuildingContextApi( typeConfiguration );
} catch ( org.hibernate.HibernateException e ) {
    if ( e.getMessage() != null && e.getMessage().contains( "not currently scoped" ) ) {
        // scope moved to SessionFactory or was released - re-acquire from the factory
    } else throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22). Data as JSON: /api/errors/17be45c9a991642e. Report an issue: GitHub.