{"record":{"id":"66ed45ff8e3c9238","repo":"hibernate/hibernate-orm","slug":"a-cte-object-with-the-name-already-exists","errorCode":null,"errorMessage":"A CTE object with the name {} already exists","messagePattern":"A CTE object with the name (.+?) 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":9516,"sourceCode":"\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) {\n\t\t\tif ( cteObjects.putIfAbsent( cteObject.getName(), cteObject ) != null ) {\n\t\t\t\tthrow new IllegalArgumentException( \"A CTE object with the name \" + cteObject.getName() + \" already exists\" );\n\t\t\t}\n\t\t}\n\t}\n\n\t@Override\n\tpublic boolean registerVisitedAssociationKey(AssociationKey associationKey) {\n\t\treturn visitedAssociationKeys.add( associationKey );\n\t}\n\n\t@Override\n\tpublic void removeVisitedAssociationKey(AssociationKey associationKey) {\n\t\tvisitedAssociationKeys.remove( associationKey );\n\t}\n\n\t@Override\n\tpublic boolean isAssociationKeyVisited(AssociationKey associationKey) {\n\t\treturn visitedAssociationKeys.contains( associationKey );\n\t}","sourceCodeStart":9498,"sourceCodeEnd":9534,"githubUrl":"https://github.com/hibernate/hibernate-orm/blob/fad1729dce015f908198d57a8d80274a30f905a5/hibernate-core/src/main/java/org/hibernate/query/sqm/sql/spi/BaseSqmToSqlAstConverter.java#L9498-L9534","documentation":"The CteObject counterpart of the duplicate-label check: addCteObject registers CTE objects (Hibernate 7's modifiable/mutation CTE objects) by name with putIfAbsent and throws IllegalArgumentException if an object with the same name is already registered in the same CTE scope.","triggerScenarios":"Programmatically registering two CteObject instances with the same name on one statement; HQL or criteria using the new modifiable-CTE feature ('with ... update/delete ... returning') twice under one name; merging containers that both carry a same-named CTE object.","commonSituations":"Adopting Hibernate 7 CTE objects for insert/update returning-style flows; name templates that collide when a statement is composed from repeated fragments.","solutions":["Give each CTE object a unique name within the statement","When composing statements from fragments, prefix object names with a scope or counter","Reuse the existing CteObject instead of registering a second one under the same name"],"exampleFix":"// before\ncontainer.addCteObject(new CteObject(\"changes\", ...));\ncontainer.addCteObject(new CteObject(\"changes\", ...));\n\n// after\ncontainer.addCteObject(new CteObject(\"changes_1\", ...));\ncontainer.addCteObject(new CteObject(\"changes_2\", ...));","handlingStrategy":"validation","validationCode":"Set<String> names = container.getCteObjects().keySet();\nif (names.contains(cteObject.getName())) {\n    throw new IllegalArgumentException(\"CTE object '\" + cteObject.getName() + \"' already exists\");\n}\ncontainer.addCteObject(cteObject);","typeGuard":null,"tryCatchPattern":"try {\n    session.createQuery(hql).executeUpdate();\n} catch (IllegalArgumentException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"CTE object\")) {\n        log.error(\"Duplicate CTE object name in: {}\", hql);\n    }\n    throw e;\n}","preventionTips":["Namespace CTE object names per statement fragment","Register each CTE object exactly once per scope","Unit-test statement composition for name collisions"],"tags":["hibernate","cte","cte-object","duplicate-name"],"backgroundTag":"duplicate-cte-name","analyzedSha":"fad1729dce015f908198d57a8d80274a30f905a5","analyzedAt":"2026-08-22T04:13:57.527Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}