{"record":{"id":"84f41b1bb91c6bdc","repo":"hibernate/hibernate-orm","slug":"named-native-query-s-specified-both-resultsetm","errorCode":null,"errorMessage":"Named native query '%s' specified both 'resultSetMapping' and an inline result set mapping","messagePattern":"Named native query '(.+?)' specified both 'resultSetMapping' and an inline result set mapping","errorType":"exception","errorClass":"AnnotationException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/boot/model/internal/QueryBinder.java","lineNumber":184,"sourceCode":"\t\tif ( namedNativeQuery != null ) {\n\t\t\tfinal String registrationName = namedNativeQuery.name();\n\t\t\tfinal String queryString = namedNativeQuery.query();\n\t\t\tif ( registrationName.isBlank() ) {\n\t\t\t\tthrow new AnnotationException(\n\t\t\t\t\t\t\"Class or package level '@NamedNativeQuery' annotation must specify a 'name'\" );\n\t\t\t}\n\n\t\t\tif ( BOOT_LOGGER.isTraceEnabled() ) {\n\t\t\t\tBOOT_LOGGER.bindingNamedNativeQuery( registrationName,\n\t\t\t\t\t\tqueryString.replace( '\\n', ' ' ) );\n\t\t\t}\n\n\t\t\tfinal var collector = context.getMetadataCollector();\n\t\t\tfinal String resultSetMappingName;\n\t\t\tif ( hasInlineResultSetMapping( namedNativeQuery ) ) {\n\t\t\t\tresultSetMappingName = registrationName;\n\t\t\t\tif ( !namedNativeQuery.resultSetMapping().isBlank() ) {\n\t\t\t\t\tthrow new AnnotationException(\n\t\t\t\t\t\t\t\"Named native query '%s' specified both 'resultSetMapping' and an inline result set mapping\"\n\t\t\t\t\t\t\t\t\t.formatted( registrationName )\n\t\t\t\t\t);\n\t\t\t\t}\n\n\t\t\t\tfinal var mappingDefinition =\n\t\t\t\t\t\tSqlResultSetMappingDescriptor.from(\n\t\t\t\t\t\t\t\tnamedNativeQuery,\n\t\t\t\t\t\t\t\tlocation == null ? null : location.getName()\n\t\t\t\t\t\t);\n\t\t\t\tif ( isDefault ) {\n\t\t\t\t\tcollector.addDefaultResultSetMapping( mappingDefinition );\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tcollector.addResultSetMapping( mappingDefinition );\n\t\t\t\t}\n\t\t\t}\n\t\t\telse {","sourceCodeStart":166,"sourceCodeEnd":202,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/boot/model/internal/QueryBinder.java#L166-L202","documentation":"A jakarta.persistence @NamedNativeQuery can define its result mapping two ways: referencing an existing @SqlResultSetMapping via resultSetMapping=, or inline via the columns attribute (@EntityResult/@ConstructorResult/@ColumnResult). hasInlineResultSetMapping detects the inline form; if resultSetMapping is also non-blank, AnnotationException is thrown because the two sources conflict.","triggerScenarios":"@NamedNativeQuery(name = \"x\", query = \"...\", resultSetMapping = \"xMapping\", columns = { @EntityResult(entityClass = Person.class) }) — both mechanisms present. Fires only when the inline mapping is detected AND the reference is non-blank.","commonSituations":"Starting with an inline mapping and later adding resultSetMapping without removing columns; merging query definitions in pull requests; copy-paste between queries that use the other style.","solutions":["Pick one style: either keep resultSetMapping and delete the columns attribute, or keep the inline columns and delete resultSetMapping.","If the mapping is shared by several native queries, extract it to @SqlResultSetMapping(name=...) and reference it with resultSetMapping only.","Add a quick review check that no @NamedNativeQuery sets both attributes."],"exampleFix":"// before\n@NamedNativeQuery(\n    name = \"Person.findNative\",\n    query = \"SELECT id, name FROM person\",\n    resultSetMapping = \"personMapping\",\n    columns = @EntityResult(entityClass = Person.class)) // conflict\n\n// after (option A: reference only)\n@NamedNativeQuery(\n    name = \"Person.findNative\",\n    query = \"SELECT id, name FROM person\",\n    resultSetMapping = \"personMapping\")\n\n// after (option B: inline only)\n@NamedNativeQuery(\n    name = \"Person.findNative\",\n    query = \"SELECT id, name FROM person\",\n    columns = @EntityResult(entityClass = Person.class))","handlingStrategy":"validation","validationCode":"@Test void nativeQueriesDeclareOneMappingStyle() {\n    for (NamedNativeQuery q : Order.class.getAnnotationsByType(NamedNativeQuery.class)) {\n        boolean inline = q.columns().length > 0;\n        boolean referenced = !q.resultSetMapping().isBlank();\n        assertTrue(!(inline && referenced),\n            q.name() + \" sets both resultSetMapping and inline columns\");\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    metadata = sources.buildMetadata();\n} catch (AnnotationException e) {\n    failBuild(\"Result mapping conflict: \" + e.getMessage());\n}","preventionTips":["Choose one mapping style per native query and enforce it in review.","Extract shared mappings to @SqlResultSetMapping and reference them by name."],"tags":["hibernate","orm","named-native-query","result-set-mapping","mapping"],"backgroundTag":"result-set-mapping-conflict","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}