{"record":{"id":"a7ff59c26d0d1782","repo":"hibernate/hibernate-orm","slug":"encountered-unexpected-content-type-s-for-named","errorCode":null,"errorMessage":"Encountered unexpected content type [%s] for named native query [%s] : [%s]","messagePattern":"Encountered unexpected content type \\[(.+?)\\] for named native query \\[(.+?)\\] : \\[(.+?)\\]","errorType":"exception","errorClass":"MappingException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/NamedQueryBinder.java","lineNumber":228,"sourceCode":"\t\t\tqueryBuilder.addParameterTypeHint( paramTypeBinding.getName(), paramTypeBinding.getType() );\n\t\t}\n\t\telse if ( content instanceof JaxbHbmSynchronizeType synchronizedSpace ) {\n\t\t\tqueryBuilder.addSynchronizedQuerySpace( synchronizedSpace.getTable() );\n\t\t}\n\t\telse if ( content instanceof JaxbHbmNativeQueryScalarReturnType scalarReturnType ) {\n\t\t\timplicitResultSetMappingBuilder.addReturn( scalarReturnType );\n\t\t}\n\t\telse if ( content instanceof JaxbHbmNativeQueryReturnType returnType ) {\n\t\t\timplicitResultSetMappingBuilder.addReturn( returnType );\n\t\t}\n\t\telse if ( content instanceof JaxbHbmNativeQueryJoinReturnType jaxbHbmNativeQueryJoinReturnType ) {\n\t\t\timplicitResultSetMappingBuilder.addReturn( jaxbHbmNativeQueryJoinReturnType );\n\t\t}\n\t\telse if ( content instanceof JaxbHbmNativeQueryCollectionLoadReturnType collectionLoadReturnType ) {\n\t\t\timplicitResultSetMappingBuilder.addReturn( collectionLoadReturnType );\n\t\t}\n\t\telse {\n\t\t\tthrow new MappingException(\n\t\t\t\t\t\"Encountered unexpected content type [%s] for named native query [%s] : [%s]\"\n\t\t\t\t\t\t\t.formatted( content.getClass().getName(), namedQueryBinding.getName(), content ),\n\t\t\t\t\tcontext.getOrigin()\n\t\t\t);\n\t\t}\n\n\t\treturn false;\n\t}\n}\n","sourceCodeStart":210,"sourceCodeEnd":238,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/NamedQueryBinder.java#L210-L238","documentation":"NamedQueryBinder walks the JAXB content list of a <sql-query> element and dispatches on type: return-scalar, return, return-join, load-collection, or plain text. Any other content object falls into the final branch and aborts bootstrap, so the message always names the Java class of the offending node. In practice this means a child element sits inside <sql-query> that Hibernate's hbm schema does not accept there.","triggerScenarios":"A child element inside <sql-query> that is not <return/>, <return-scalar/>, <return-join/> or <load-collection/> - for example a stray <resultset>, a misspelled element name, or an element from another schema version or namespace. The document reached the binder only because it was not validated against the XSD beforehand.","commonSituations":"Hand-edited or tool-merged hbm.xml with an element nested under the wrong parent; upgrades where an older Hibernate mapping uses syntax the current hbm schema no longer accepts in that position; mappings generated by string concatenation.","solutions":["Inspect the direct children of the <sql-query> named in the message; only <return/>, <return-scalar/>, <return-join/> and <load-collection/> are allowed there, with the SQL itself as element text","Move or remove the offending element (e.g. a named <resultset> belongs at the top level of the mapping, not inside the query)","Validate the whole document against the hbm XSD for your Hibernate version (e.g. xmllint --schema) to surface the structural error with line numbers"],"exampleFix":"// before\n<sql-query name='findPersons'>\n    <resultset name='unused'/>\n    <return-scalar column='id'/>\n    select id, name from Person\n</sql-query>\n\n// after\n<sql-query name='findPersons'>\n    <return-scalar column='id'/>\n    select id, name from Person\n</sql-query>","handlingStrategy":"validation","validationCode":"// Validate hbm.xml against the hbm XSD shipped with your Hibernate version before bootstrap\nvar factory = javax.xml.parsers.DocumentBuilderFactory.newInstance();\nfactory.setNamespaceAware(true);\nfactory.setSchema(javax.xml.validation.SchemaFactory\n        .newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI)\n        .newSchema(xsdFile));\nfactory.newDocumentBuilder().parse(hbmFile); // misplaced elements fail here, with line numbers","typeGuard":null,"tryCatchPattern":"catch (org.hibernate.boot.MappingException e) during Metadata building: the message contains the unexpected content class name and the query name - use the class name to identify which element was misplaced, then fix the document; do not swallow, bootstrap cannot proceed without a valid mapping","preventionTips":["Keep hbm files XSD-validated in CI (xmllint --schema) so misplaced elements never reach the binder","Generate mappings with schema-aware tooling rather than string templates","After a Hibernate upgrade, re-validate every hbm.xml against the new XSD before deploying"],"tags":["hibernate","hbm","native-query","mapping","orm"],"backgroundTag":"unsupported-mapping-element","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}