{"record":{"id":"14c046c8e434a9f7","repo":"hibernate/hibernate-orm","slug":"named-query-s-did-not-specify-query-string","errorCode":null,"errorMessage":"Named query [%s] did not specify query string","messagePattern":"Named query \\[(.+?)\\] did not specify query string","errorType":"exception","errorClass":"MappingException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/NamedQueryBinder.java","lineNumber":77,"sourceCode":"\n\t\tfor ( Object content : namedQueryBinding.getContent() ) {\n\t\t\tif ( content instanceof String string ) {\n\t\t\t\tif ( isNotBlank( string ) ) {\n\t\t\t\t\tqueryBuilder.setHqlString( string.trim() );\n\t\t\t\t\tfoundQuery = true;\n\t\t\t\t}\n\t\t\t}\n\t\t\telse if ( content instanceof JAXBElement<?> element\n\t\t\t\t\t&& element.getValue() instanceof JaxbHbmQueryParamType queryParamType ) {\n\t\t\t\tqueryBuilder.addParameterTypeHint( queryParamType.getName(), queryParamType.getType() );\n\t\t\t}\n\t\t\telse {\n\t\t\t\tthrow new AssertionFailure( \"Unexpected content type: \" + content.getClass().getName() );\n\t\t\t}\n\t\t}\n\n\t\tif ( !foundQuery ) {\n\t\t\tthrow new MappingException(\n\t\t\t\t\t\"Named 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\tcontext.getMetadataCollector().addNamedQuery( queryBuilder.build() );\n\t}\n\n\n\t// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\t// Named native query\n\n\tpublic static void processNamedNativeQuery(\n\t\t\tHbmLocalMetadataBuildingContext context,\n\t\t\tJaxbHbmNamedNativeQueryType namedQueryBinding) {\n\t\tprocessNamedNativeQuery( context, namedQueryBinding, \"\" );\n\t}","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/boot/model/source/internal/hbm/NamedQueryBinder.java#L59-L95","documentation":"A named HQL query (<query name='...'>...</query> in hbm.xml) must contain the query text, either as the element's text content or via recognized content items. The binder tracks foundQuery while walking the element's content; if nothing it processed was the query text (element empty, whitespace-only, or containing only query-param/hint elements), it throws this MappingException when adding the named query.","triggerScenarios":"<query name='usersByName'/> with no body; the HQL was externalized or commented out leaving the element empty; CDATA mangled during file edits so the text node is lost.","commonSituations":"Editing mapping files and accidentally emptying the query body; templating or generation steps leaving placeholder queries empty; migration tooling dropping CDATA sections.","solutions":["Put the HQL text inside the <query> element: <query name='usersByName'>from User u where u.name = :name</query>","If the body uses CDATA, verify the CDATA section survived editing","Add a build-time check that every <query> element has non-blank text content"],"exampleFix":"// before\n<query name='usersByName'/>\n\n// after\n<query name='usersByName'><![CDATA[from User u where u.name = :name]]></query>","handlingStrategy":"validation","validationCode":"NodeList queries = doc.getElementsByTagName(\"query\");\nfor (int i = 0; i < queries.getLength(); i++) {\n    Element q = (Element) queries.item(i);\n    boolean hasText = q.getTextContent() != null && !q.getTextContent().isBlank();\n    if (!hasText) {\n        throw new IllegalStateException(\"named query '\" + q.getAttribute(\"name\") + \"' has no query text\");\n    }\n}","typeGuard":null,"tryCatchPattern":"catch (MappingException e) at bootstrap; the message names the empty named query. Fill in its HQL body (CDATA is fine) and rebuild.","preventionTips":["Externalize HQL to annotations or a dedicated file with a lint step rather than inline stubs","Assert every <query> element has non-blank text in CI","Avoid commenting out query bodies instead of removing the element"],"tags":["hibernate","hbm-mapping","named-query","hql","empty-query"],"backgroundTag":"empty-named-query","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}