{"record":{"id":"c1fc9bb95c0db280","repo":"hibernate/hibernate-orm","slug":"invalid-query-type-provided-to-subquery-with-met","errorCode":null,"errorMessage":"Invalid query type provided to subquery 'with' method, expecting a subquery with the same parent to use as CTE","messagePattern":"Invalid query type provided to subquery 'with' method, expecting a subquery with the same parent to use as CTE","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/query/sqm/tree/spi/select/SqmSubQuery.java","lineNumber":212,"sourceCode":"\t\t\t\t? cteContainer.getCteStatement( cteLabel )\n\t\t\t\t: cteCriteria;\n\t}\n\n\t@Override\n\tpublic <X> @Nullable JpaCteCriteria<X> getCteCriteria(@Nonnull String cteName) {\n\t\tfinal JpaCteCriteria<X> cteCriteria = super.getCteCriteria( cteName );\n\t\treturn cteCriteria == null && parent instanceof JpaCteContainer cteContainer\n\t\t\t\t? cteContainer.getCteCriteria( cteName )\n\t\t\t\t: cteCriteria;\n\t}\n\n\t@Override\n\tprotected <X> JpaCteCriteria<X> withInternal(String name, AbstractQuery<X> criteria) {\n\t\tif ( criteria instanceof SqmSubQuery<X> sqmSubQuery && sqmSubQuery.getParent() == parent ) {\n\t\t\treturn super.withInternal( name, criteria );\n\t\t}\n\t\telse {\n\t\t\tthrow new IllegalArgumentException(\n\t\t\t\t\t\"Invalid query type provided to subquery 'with' method, \" +\n\t\t\t\t\t\"expecting a subquery with the same parent to use as CTE\"\n\t\t\t);\n\t\t}\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>, AbstractQuery<X>> recursiveCriteriaProducer) {\n\t\tif ( baseCriteria instanceof SqmSubQuery<X> sqmSubQuery && sqmSubQuery.getParent() == parent ) {\n\t\t\treturn super.withInternal( name, baseCriteria, unionDistinct, recursiveCriteriaProducer );\n\t\t}\n\t\telse {\n\t\t\tthrow new IllegalArgumentException(\n\t\t\t\t\t\"Invalid query type provided to subquery 'with' method, \" +","sourceCodeStart":194,"sourceCodeEnd":230,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/query/sqm/tree/spi/select/SqmSubQuery.java#L194-L230","documentation":"Error \"Invalid query type provided to subquery 'with' method, expecting a subquery with the same parent to use as CTE\" thrown in hibernate/hibernate-orm.","triggerScenarios":"An incompatible query (wrong parent or root query) is passed to a subquery's 'with' method for a CTE.","commonSituations":"Reusing a subquery from another parent as CTE; create the CTE subquery from the same parent.","solutions":["Pass a subquery that has the same parent query as the one 'with' is called on.","Create the CTE subquery via Subquery#with on a subquery obtained from the same parent, not an unrelated one.","If the CTE belongs to the root query, register it on the root query's 'with' method instead."],"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"}