{"record":{"id":"1c6f71012b581496","repo":"hibernate/hibernate-orm","slug":"buildnamedqueryrepository-should-not-be-called-on","errorCode":null,"errorMessage":"#buildNamedQueryRepository should not be called on InFlightMetadataCollector","messagePattern":"#buildNamedQueryRepository should not be called on InFlightMetadataCollector","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/boot/internal/InFlightMetadataCollectorImpl.java","lineNumber":258,"sourceCode":"\t}\n\n\t@Override\n\tpublic SqmFunctionRegistry getFunctionRegistry() {\n\t\treturn bootstrapContext.getFunctionRegistry();\n\t}\n\n\t@Override\n\tpublic Database getDatabase() {\n\t\t// important to delay this instantiation until as late as possible.\n\t\tif ( database == null ) {\n\t\t\tdatabase = new Database( options );\n\t\t}\n\t\treturn database;\n\t}\n\n\t@Override\n\tpublic NamedObjectRepository buildNamedQueryRepository() {\n\t\tthrow new UnsupportedOperationException( \"#buildNamedQueryRepository should not be called on InFlightMetadataCollector\" );\n\t}\n\n\t@Override\n\tpublic List<PersistenceUnitCallbackDefinition> getPersistenceUnitLifecycleCallbackDefinitions() {\n\t\treturn PersistenceUnitCallbackDefinition.from(\n\t\t\t\tglobalRegistrations.getPersistenceUnitLifecycleEventHandlers() );\n\t}\n\n\t@Override\n\tpublic Map<String, SqmFunctionDescriptor> getSqlFunctionMap() {\n\t\treturn sqlFunctionMap;\n\t}\n\n\t@Override\n\tpublic Set<String> getContributors() {\n\t\tthrow new UnsupportedOperationException();\n\t}\n","sourceCodeStart":240,"sourceCodeEnd":276,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/boot/internal/InFlightMetadataCollectorImpl.java#L240-L276","documentation":"InFlightMetadataCollectorImpl is the write-time view of metadata during bootstrap; #buildNamedQueryRepository is a terminal operation that Hibernate itself calls on the completed MetadataImplementor at the end of bootstrap. Calling it on the in-flight collector throws UnsupportedOperationException by design, to flag API misuse.","triggerScenarios":"Code that receives metadata during bootstrap - custom Integrators, MetadataSourceProcessors, or anything holding the InFlightMetadataCollectorImpl passed through those SPIs - and invokes buildNamedQueryRepository() on it before MetadataBuilder#build() has finished.","commonSituations":"Custom integrations trying to inspect named queries mid-bootstrap; frameworks or tutorials written against older/less-segmented Hibernate metadata APIs; upgrades where a previously tolerated call now fails fast.","solutions":["Move the call to after bootstrap, on the Metadata instance returned by MetadataBuilder#build()","If you must hook mid-bootstrap, use InFlightMetadataCollectorDelegate and only touch in-flight state, never terminal operations","Check the stack trace for the integration making the call and upgrade it to a Hibernate-compatible version"],"exampleFix":"// before: terminal call during bootstrap\npublic void contribute(InFlightMetadataCollector metadata) {\n    metadata.buildNamedQueryRepository(); // UnsupportedOperationException\n}\n\n// after: operate on completed metadata\nMetadata completed = metadataBuilder.build();\nNamedObjectRepository repo = ((MetadataImplementor) completed).buildNamedQueryRepository();","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"if (metadata instanceof InFlightMetadataCollector) {\n    return; // bootstrap still in progress: defer all terminal operations\n}\nNamedObjectRepository repo = ((MetadataImplementor) metadata).buildNamedQueryRepository();","tryCatchPattern":"try {\n    metadata.buildNamedQueryRepository();\n}\ncatch (UnsupportedOperationException e) {\n    // wrong phase: re-run the logic on the Metadata returned by MetadataBuilder#build()\n}","preventionTips":["Only call terminal Metadata operations on the instance returned by MetadataBuilder#build()","Write integrations against InFlightMetadataCollectorDelegate, which mirrors the in-flight API"],"tags":["hibernate","internal-api","metadata","bootstrap"],"backgroundTag":"premature-api-call","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}