{"record":{"id":"1cc8fd29d64db156","repo":"hibernate/hibernate-orm","slug":"named-native-query-s-specified-both-a-resultset","errorCode":null,"errorMessage":"Named native query [%s] specified both a resultset-ref and an inline mapping of results","messagePattern":"Named native query \\[(.+?)\\] specified both a resultset-ref and an inline mapping of results","errorType":"exception","errorClass":"MappingException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/NamedQueryBinder.java","lineNumber":146,"sourceCode":"\t\t\t\t\tcontext\n\t\t\t);\n\t\t\tif ( wasQuery ) {\n\t\t\t\tfoundQuery = true;\n\t\t\t}\n\t\t}\n\t\tif ( !foundQuery ) {\n\t\t\tthrow new MappingException(\n\t\t\t\t\t\"Named native query [%s] did not specify query string\"\n\t\t\t\t\t\t\t.formatted( namedQueryBinding.getName() ),\n\t\t\t\t\tcontext.getOrigin()\n\t\t\t);\n\t\t}\n\n\t\tfinal var collector = context.getMetadataCollector();\n\n\t\tif ( implicitResultSetMappingBuilder.hasAnyReturns() ) {\n\t\t\tif ( isNotEmpty( namedQueryBinding.getResultsetRef() ) ) {\n\t\t\t\tthrow new MappingException(\n\t\t\t\t\t\t\"Named native query [%s] specified both a resultset-ref and an inline mapping of results\"\n\t\t\t\t\t\t\t\t.formatted( namedQueryBinding.getName() ),\n\t\t\t\t\t\tcontext.getOrigin()\n\t\t\t\t);\n\t\t\t}\n\n\t\t\tcollector.addResultSetMapping( implicitResultSetMappingBuilder.build( context ) );\n\t\t\tbuilder.setResultSetMappingName( implicitResultSetMappingBuilder.getRegistrationName() );\n\t\t}\n\n\t\tif ( namedQueryBinding.isCallable() ) {\n\t\t\tfinal var definition =\n\t\t\t\t\tcreateStoredProcedure( builder, context,\n\t\t\t\t\t\t\t() -> illegalCallSyntax( context, namedQueryBinding, builder.getSqlString() ) );\n\t\t\tcollector.addNamedProcedureCallDefinition( definition );\n\t\t\tDEPRECATION_LOGGER.callableNamedNativeQuery();\n\t\t}\n\t\telse {","sourceCodeStart":128,"sourceCodeEnd":164,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/NamedQueryBinder.java#L128-L164","documentation":"Hibernate binds each hbm.xml <sql-query> by folding its inline return elements (<return/>, <return-scalar/>, <return-join/>, <load-collection/>) into an implicit result-set mapping. A named native query may describe its results either through that inline mapping or by referencing a separately declared <resultset> via the resultset-ref attribute - never both. When at least one inline return exists and resultset-ref is also non-empty, NamedQueryBinder aborts bootstrap with this MappingException.","triggerScenarios":"An hbm.xml contains <sql-query name='findAll' resultset-ref='personRs'> that also declares at least one inline return child such as <return-scalar column='id'/> or <return alias='p' class='com.Person'/>. Thrown during Metadata building (SessionFactory bootstrap), before any query executes.","commonSituations":"Migrating between inline returns and shared named <resultset> definitions and leaving both halves in the file; merging two query definitions during copy-paste; hand-maintained legacy hbm.xml that never went through XSD validation.","solutions":["Choose one style: delete the resultset-ref attribute and keep the inline <return*> elements, or delete the inline elements and keep resultset-ref pointing at an existing <resultset name='personRs'> definition","Verify the referenced <resultset> actually exists in the same document or an included one","Validate the corrected file against the Hibernate hbm XSD to catch similar conflicting declarations"],"exampleFix":"// before - hbm.xml\n<sql-query name='findAll' resultset-ref='personRs'>\n    <return-scalar column='id'/>\n    <return-scalar column='name'/>\n</sql-query>\n\n// after - inline mapping kept, resultset-ref removed\n<sql-query name='findAll'>\n    <return-scalar column='id'/>\n    <return-scalar column='name'/>\n</sql-query>","handlingStrategy":"validation","validationCode":"// Pre-flight scan of every hbm.xml before building the SessionFactory\nvar doc = javax.xml.parsers.DocumentBuilderFactory.newInstance()\n        .newDocumentBuilder().parse(hbmFile);\nvar queries = doc.getElementsByTagName(\"sql-query\");\nfor (int i = 0; i < queries.getLength(); i++) {\n    var q = (org.w3c.dom.Element) queries.item(i);\n    boolean hasRef = !q.getAttribute(\"resultset-ref\").isEmpty();\n    boolean hasInline = q.getElementsByTagName(\"return\").getLength() > 0\n            || q.getElementsByTagName(\"return-scalar\").getLength() > 0\n            || q.getElementsByTagName(\"return-join\").getLength() > 0\n            || q.getElementsByTagName(\"load-collection\").getLength() > 0;\n    if (hasRef && hasInline)\n        throw new IllegalStateException(\"sql-query '\" + q.getAttribute(\"name\")\n                + \"' mixes resultset-ref with inline returns\");\n}","typeGuard":null,"tryCatchPattern":"try { Metadata metadata = metadataBuilder.build(); } catch (org.hibernate.boot.MappingException e) { /* message names the offending query; getOrigin() points at the hbm.xml source - surface both as a configuration error and stop startup */ throw new IllegalStateException(\"Bad native-query mapping: \" + e.getMessage(), e); }","preventionTips":["Run xmllint --schema with the Hibernate hbm XSD over all mapping files in CI","Standardize on one native-query result-mapping style (inline vs resultset-ref) per codebase and lint for the other","When refactoring to resultset-ref, grep the file for <return before deleting the <resultset> definition, and vice versa"],"tags":["hibernate","hbm","native-query","resultset-mapping","orm"],"backgroundTag":"conflicting-mapping-attributes","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}