{"record":{"id":"f6f41411eecf41dd","repo":"hibernate/hibernate-orm","slug":"expecting-a-selection-query-but-found","errorCode":null,"errorMessage":"Expecting a selection query, but found '{}'","messagePattern":"Expecting a selection query, but found '(.+?)'","errorType":"exception","errorClass":"IllegalSelectQueryException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/internal/AbstractSharedSessionContract.java","lineNumber":2075,"sourceCode":"\t\treturn getMappingMetamodel().getEntityDescriptor( entityName );\n\t}\n\n\t// Hibernate Reactive may need to use this\n\tprotected final CollectionPersister requireCollectionPersister(String roleName) {\n\t\treturn getMappingMetamodel().getCollectionDescriptor( roleName );\n\t}\n\n\tprotected HqlInterpretation<?> interpretHql(String hql) {\n\t\treturn interpretHql( hql, null );\n\t}\n\n\tprotected <R> HqlInterpretation<R> interpretHql(String hql, Class<R> resultType) {\n\t\treturn getFactory().getQueryEngine().interpretHql( hql, resultType );\n\t}\n\n\tprotected static void checkSelectionQuery(String hql, HqlInterpretation<?> hqlInterpretation) {\n\t\tif ( !( hqlInterpretation.getSqmStatement() instanceof SqmSelectStatement ) ) {\n\t\t\tthrow new IllegalSelectQueryException( \"Expecting a selection query, but found '\" + hql + \"'\", hql);\n\t\t}\n\t}\n\n\tprotected static <R> void checkResultType(Class<R> expectedResultType, SelectionQuery<R> query) {\n\t\tfinal var resultType = query.getResultType();\n\t\tif ( !expectedResultType.isAssignableFrom( resultType ) ) {\n\t\t\tthrow new QueryTypeMismatchException(\n\t\t\t\t\tString.format(\n\t\t\t\t\t\t\tLocale.ROOT,\n\t\t\t\t\t\t\t\"Incorrect query result type: query produces '%s' but type '%s' was given\",\n\t\t\t\t\t\t\texpectedResultType.getName(),\n\t\t\t\t\t\t\tresultType.getName()\n\t\t\t\t\t)\n\t\t\t);\n\t\t}\n\t}\n\n\tprotected NamedResultSetMappingMemento getResultSetMappingMemento(String resultSetMappingName) {","sourceCodeStart":2057,"sourceCodeEnd":2093,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/internal/AbstractSharedSessionContract.java#L2057-L2093","documentation":"Selection-query APIs (createSelectionQuery and typed/named variants) pass through checkSelectionQuery(): the interpreted SQM statement must be an SqmSelectStatement. An UPDATE/DELETE/INSERT string yields IllegalSelectQueryException('Expecting a selection query, but found <hql>'). Like error 1553, this guards the selection/mutation API split so result-list semantics are guaranteed.","triggerScenarios":"session.createSelectionQuery(\"update Account a set a.locked = true\"); createSelectionQuery(hql, type) with DML; createNamedQuery(name, type) where the named query is defined as an update/delete; generic helpers funneling all strings into selection creation.","commonSituations":"Shared DAO query methods that build HQL dynamically and pick the wrong factory; porting Hibernate 5 code where Query served both roles; a malformed select that parses as DML (missing 'from' or a stray 'update' keyword).","solutions":["Route DML to createMutationQuery/executeUpdate; use createSelectionQuery only for SELECT.","Fix the HQL when a select was intended but the statement parses as DML.","For named queries, match the creation API to the kind declared in @NamedQuery."],"exampleFix":"// before\nSelectionQuery<?> q = session.createSelectionQuery(\"update Account a set a.locked = true\"); // throws\n// after\nMutationQuery q = session.createMutationQuery(\"update Account a set a.locked = true\");\nq.executeUpdate();","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"static boolean isSelectHql(String hql) {\n    String head = hql.stripLeading().toLowerCase(Locale.ROOT);\n    return !(head.startsWith(\"update \") || head.startsWith(\"delete \") || head.startsWith(\"insert \"));\n}\n\n// usage\nif (isSelectHql(hql)) {\n    results = session.createSelectionQuery(hql, type).list();\n} else {\n    session.createMutationQuery(hql).executeUpdate();\n}","tryCatchPattern":"try {\n    return session.createSelectionQuery(hql, type).list();\n} catch (IllegalSelectQueryException e) {\n    throw new IllegalArgumentException(\"Expected SELECT but got: \" + hql, e);\n}","preventionTips":["Pick the creation API by statement kind, not convenience","Keep dynamically-built HQL builders honest about which kind they emit","Align @NamedQuery definitions with the API used to load them"],"tags":["hibernate","hql","selection-query","query-api","orm"],"backgroundTag":"query-type-mismatch","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}