hibernate/hibernate-orm · error · IllegalArgumentException

A CTE with the label " + tableExpression + " already exists

Error message

A CTE with the label " + tableExpression + " already exists

What it means

Error "A CTE with the label " + tableExpression + " already exists" thrown in hibernate/hibernate-orm.

Source

Thrown at hibernate-core/src/main/java/org/hibernate/sql/ast/tree/AbstractStatement.java:53

	@Override
	public Map<String, CteStatement> getCteStatements() {
		return cteStatements;
	}

	@Override
	public CteStatement getCteStatement(String cteLabel) {
		final var cteStatement = cteStatements.get( cteLabel );
		return cteStatement == null && parentCteContainer != null
				? parentCteContainer.getCteStatement( cteLabel )
				: cteStatement;
	}

	@Override
	public void addCteStatement(CteStatement cteStatement) {
		final String tableExpression = cteStatement.getCteTable().getTableExpression();
		if ( cteStatements.putIfAbsent( tableExpression, cteStatement ) != null ) {
			throw new IllegalArgumentException( "A CTE with the label " + tableExpression + " already exists" );
		}
	}

	@Override
	public Map<String, CteObject> getCteObjects() {
		return cteObjects;
	}

	@Override
	public CteObject getCteObject(String cteObjectName) {
		final var cteObject = cteObjects.get( cteObjectName );
		return cteObject == null
			&& parentCteContainer != null
				? parentCteContainer.getCteObject( cteObjectName )
				: cteObject;
	}

	@Override

View on GitHub (pinned to fad1729dce)

Solutions

  1. Use a unique label for each CTE (WITH clause) in the query; a CTE with the same table expression label was already registered.
  2. Rename the conflicting CTE or remove the duplicate WITH clause definition.

When it happens

Trigger: A CTE is registered under a label that is already present in the statement: " + tableExpression + ".

Common situations: Building a query that defines two common table expressions with the same name/label, e.g. programmatically generated CTEs colliding.


AI-assisted analysis of hibernate/hibernate-orm@fad1729dce (2026-08-22). Data as JSON: /api/errors/08bafbef446ec21c. Report an issue: GitHub.