hibernate/hibernate-orm · error · UnsupportedOperationException

You should not be building a SessionFactory from an in-fligh

Error message

You should not be building a SessionFactory from an in-flight metadata collector; and of course we should better segment this in the API :)

What it means

Hibernate's bootstrap is two-phase: an in-flight collector accumulates mappings, and only the completed Metadata may participate in SessionFactory wiring. initSessionFactory(sessionFactory) is part of that wiring, and InFlightMetadataCollectorImpl throws UnsupportedOperationException with the 'should not be building a SessionFactory from an in-flight metadata collector' message to make the phase violation explicit.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/boot/internal/InFlightMetadataCollectorImpl.java:294

	@Override
	public void orderColumns(boolean forceOrdering) {
		// nothing to do
	}

	@Override
	public void validate() throws MappingException {
		// nothing to do
	}

	@Override
	public Set<MappedSuperclass> getMappedSuperclassMappingsCopy() {
		return new HashSet<>( mappedSuperClasses.values() );
	}

	@Override
	public void initSessionFactory(SessionFactoryImplementor sessionFactory) {
		throw new UnsupportedOperationException(
				"""
				You should not be building a SessionFactory from an in-flight metadata collector; \
				and of course we should better segment this in the API :)
				"""
		);
	}

	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// Composite handling

	@Override
	public void registerComponent(Component component) {
		composites.add( component );
	}

	@Override
	public void visitRegisteredComponents(Consumer<Component> consumer) {
		composites.forEach( consumer );

View on GitHub (pinned to fad1729dce)

Solutions

  1. Build the SessionFactory only from the Metadata returned by MetadataBuilder#build() using the standard two-phase bootstrap
  2. Restructure integrations to contribute during bootstrap and to consume metadata only after it completes
  3. Update the integration or library that performs the call to a version compatible with your Hibernate version

Example fix

// before: inside an Integrator during bootstrap
((MetadataImplementor) inFlightMetadata).initSessionFactory(sessionFactory);

// after: standard two-phase bootstrap; wiring happens internally
Metadata metadata = metadataBuilder.build();
SessionFactory sf = metadata.getSessionFactoryBuilder().build();
Defensive patterns

Strategy: type-guard

Type guard

if (metadata instanceof InFlightMetadataCollectorImpl) {
    throw new IllegalStateException(
            "metadata not built yet - initSessionFactory is only valid on completed metadata");
}
((MetadataImplementor) metadata).initSessionFactory(sessionFactory);

Prevention

When it happens

Trigger: An Integrator or custom bootstrap component receives the in-flight metadata during MetadataBuilder#build() and calls initSessionFactory(...) on it, or code casts an InFlightMetadataCollector to MetadataImplementor and drives SessionFactory initialization itself.

Common situations: Custom framework glue trying to attach a SessionFactory to metadata inside an integration callback; code written against older Hibernate versions where the Metadata API was less segmented; test harnesses re-using bootstrap objects across phases.

Related errors


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