{"record":{"id":"39261a158e45f764","repo":"hibernate/hibernate-orm","slug":"you-should-not-be-building-a-sessionfactory-from-a","errorCode":null,"errorMessage":"You should not be building a SessionFactory from an in-flight metadata collector; and of course we should better segment this in the API :)","messagePattern":"You should not be building a SessionFactory from an in-flight metadata collector; and of course we should better segment this in the API :\\)","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/boot/internal/InFlightMetadataCollectorImpl.java","lineNumber":294,"sourceCode":"\n\t@Override\n\tpublic void orderColumns(boolean forceOrdering) {\n\t\t// nothing to do\n\t}\n\n\t@Override\n\tpublic void validate() throws MappingException {\n\t\t// nothing to do\n\t}\n\n\t@Override\n\tpublic Set<MappedSuperclass> getMappedSuperclassMappingsCopy() {\n\t\treturn new HashSet<>( mappedSuperClasses.values() );\n\t}\n\n\t@Override\n\tpublic void initSessionFactory(SessionFactoryImplementor sessionFactory) {\n\t\tthrow new UnsupportedOperationException(\n\t\t\t\t\"\"\"\n\t\t\t\tYou should not be building a SessionFactory from an in-flight metadata collector; \\\n\t\t\t\tand of course we should better segment this in the API :)\n\t\t\t\t\"\"\"\n\t\t);\n\t}\n\n\t// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\t// Composite handling\n\n\t@Override\n\tpublic void registerComponent(Component component) {\n\t\tcomposites.add( component );\n\t}\n\n\t@Override\n\tpublic void visitRegisteredComponents(Consumer<Component> consumer) {\n\t\tcomposites.forEach( consumer );","sourceCodeStart":276,"sourceCodeEnd":312,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/boot/internal/InFlightMetadataCollectorImpl.java#L276-L312","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Build the SessionFactory only from the Metadata returned by MetadataBuilder#build() using the standard two-phase bootstrap","Restructure integrations to contribute during bootstrap and to consume metadata only after it completes","Update the integration or library that performs the call to a version compatible with your Hibernate version"],"exampleFix":"// before: inside an Integrator during bootstrap\n((MetadataImplementor) inFlightMetadata).initSessionFactory(sessionFactory);\n\n// after: standard two-phase bootstrap; wiring happens internally\nMetadata metadata = metadataBuilder.build();\nSessionFactory sf = metadata.getSessionFactoryBuilder().build();","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"if (metadata instanceof InFlightMetadataCollectorImpl) {\n    throw new IllegalStateException(\n            \"metadata not built yet - initSessionFactory is only valid on completed metadata\");\n}\n((MetadataImplementor) metadata).initSessionFactory(sessionFactory);","tryCatchPattern":null,"preventionTips":["Use the two-phase bootstrap: MetadataBuilder#build(), then getSessionFactoryBuilder().build()","Never wire a SessionFactory into metadata received inside an Integrator callback"],"tags":["hibernate","session-factory","metadata","bootstrap"],"backgroundTag":"premature-api-call","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}