{"record":{"id":"c90fad11081b6714","repo":"hibernate/hibernate-orm","slug":"a-cte-with-the-label-already-exists","errorCode":null,"errorMessage":"A CTE with the label {} already exists","messagePattern":"A CTE with the label (.+?) already exists","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hibernate-core/src/main/java/org/hibernate/query/sqm/sql/spi/BaseSqmToSqlAstConverter.java","lineNumber":9496,"sourceCode":"\n\t\t@Override\n\t\tpublic Map<String, CteStatement> getCteStatements() {\n\t\t\treturn cteStatements;\n\t\t}\n\n\t\t@Override\n\t\tpublic CteStatement getCteStatement(String cteLabel) {\n\t\t\tfinal var cteStatement = cteStatements.get( cteLabel );\n\t\t\treturn cteStatement == null && parent != null\n\t\t\t\t\t? parent.getCteStatement( cteLabel )\n\t\t\t\t\t: cteStatement;\n\t\t}\n\n\t\t@Override\n\t\tpublic void addCteStatement(CteStatement cteStatement) {\n\t\t\tfinal String tableExpression = cteStatement.getCteTable().getTableExpression();\n\t\t\tif ( cteStatements.putIfAbsent( tableExpression, cteStatement ) != null ) {\n\t\t\t\tthrow new IllegalArgumentException( \"A CTE with the label \" + tableExpression + \" already exists\" );\n\t\t\t}\n\t\t}\n\n\t\t@Override\n\t\tpublic Map<String, CteObject> getCteObjects() {\n\t\t\treturn cteObjects;\n\t\t}\n\n\t\t@Override\n\t\tpublic CteObject getCteObject(String cteObjectName) {\n\t\t\tfinal var cteObject = cteObjects.get( cteObjectName );\n\t\t\treturn cteObject == null && parent != null\n\t\t\t\t\t? parent.getCteObject( cteObjectName )\n\t\t\t\t\t: cteObject;\n\t\t}\n\n\t\t@Override\n\t\tpublic void addCteObject(CteObject cteObject) {","sourceCodeStart":9478,"sourceCodeEnd":9514,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/spi/BaseSqmToSqlAstConverter.java#L9478-L9514","documentation":"The CteContainer implementation keys its statements by the CTE table expression (the label) and registers them with putIfAbsent. addCteStatement throws IllegalArgumentException when the key already exists in the same CTE scope - i.e. two CTE statements declare the same label in one statement scope.","triggerScenarios":"HQL 'with t as (...), t as (...) select ...'; criteria code calling with(...) twice with the same name on one query; merging subquery CTE containers that both declare the same label into one scope.","commonSituations":"Copy-paste of with-clause fragments; generated queries concatenating CTE blocks with a fixed label like 'data' or 'ids'; refactor of shared CTE snippets into one statement.","solutions":["Rename one of the duplicate CTE labels so each is unique within the statement","Merge the two same-named CTEs if they are logically the same definition","When building CTEs programmatically, derive names from a counter or the content to guarantee uniqueness"],"exampleFix":"// before\nwith ids as (select 1), ids as (select 2) select * from ids\n\n// after\nwith ids_a as (select 1), ids_b as (select 2) select * from ids_a","handlingStrategy":"validation","validationCode":"// Before adding: check the label is free in this scope\nif (java.util.Arrays.stream(existingContainer.getCteStatements().toArray())\n        .anyMatch(s -> ((SqmCteStatement<?>) s).getCteTable().getCteName().equals(label))) {\n    throw new IllegalArgumentException(\"CTE label '\" + label + \"' already exists\");\n}\nexistingContainer.addCteStatement(newStatement);","typeGuard":null,"tryCatchPattern":"try {\n    session.createQuery(hql).executeUpdate();\n} catch (IllegalArgumentException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"already exists\")) {\n        log.error(\"Duplicate CTE label in: {}\", hql);\n    }\n    throw e;\n}","preventionTips":["Generate CTE labels from a per-statement counter or content hash","Lint with-clauses for duplicate labels before executing assembled HQL","Share CTE definitions via constants, one declaration point per query"],"tags":["hibernate","cte","duplicate-name","hql"],"backgroundTag":"duplicate-cte-name","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}