{"record":{"id":"cd70d9551d34e287","repo":"hibernate/hibernate-orm","slug":"invalid-query-type-provided-to-root-query-with-m","errorCode":null,"errorMessage":"Invalid query type provided to root query 'with' method, expecting a root query to use as CTE instead found a subquery","messagePattern":"Invalid query type provided to root query 'with' method, expecting a root query to use as CTE instead found a subquery","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/query/sqm/tree/spi/select/SqmSelectStatement.java","lineNumber":279,"sourceCode":"\t}\n\n\t@Override\n\tpublic <X> X accept(SemanticQueryWalker<X> walker) {\n\t\treturn walker.visitSelectStatement( this );\n\t}\n\n\t@Override\n\tpublic void addParameter(SqmParameter<?> parameter) {\n\t\tif ( parameters == null ) {\n\t\t\tparameters = new LinkedHashSet<>();\n\t\t}\n\t\tparameters.add( parameter );\n\t}\n\n\t@Override\n\tprotected <X> JpaCteCriteria<X> withInternal(String name, AbstractQuery<X> criteria) {\n\t\tif ( criteria instanceof SqmSubQuery<?> ) {\n\t\t\tthrow new IllegalArgumentException(\n\t\t\t\t\t\"Invalid query type provided to root query 'with' method, \"\n\t\t\t\t\t+ \"expecting a root query to use as CTE instead found a subquery\"\n\t\t\t);\n\t\t}\n\t\treturn super.withInternal( name, criteria );\n\t}\n\n\t@Override\n\tprotected <X> JpaCteCriteria<X> withInternal(\n\t\t\tString name,\n\t\t\tAbstractQuery<X> baseCriteria,\n\t\t\tboolean unionDistinct,\n\t\t\tFunction<JpaCteCriteria<X>,\n\t\t\tAbstractQuery<X>> recursiveCriteriaProducer) {\n\t\tif ( baseCriteria instanceof SqmSubQuery<?> ) {\n\t\t\tthrow new IllegalArgumentException(\n\t\t\t\t\t\"Invalid query type provided to root query 'with' method, \"\n\t\t\t\t\t+ \"expecting a root query to use as CTE instead found a subquery\"","sourceCodeStart":261,"sourceCodeEnd":297,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/spi/select/SqmSelectStatement.java#L261-L297","documentation":"Error \"Invalid query type provided to root query 'with' method, expecting a root query to use as CTE instead found a subquery\" thrown in hibernate/hibernate-orm.","triggerScenarios":"A subquery is passed to the 'with' method of a root query, which only accepts a root query as CTE.","commonSituations":"Mixing a CriteriaSubquery into with(); build the CTE as a root-level criteria query instead.","solutions":["Pass a root query (not a subquery) to the root query 'with' method when registering a CTE.","Build the CTE as a top-level criteria query and pass that instance.","If the CTE is derived from another query, create a fresh root query with the same structure rather than reusing a subquery."],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}