Skip to content

Qualify child aggregate columns with the table they are selected from - #2337

Open
Develop-KIM wants to merge 1 commit into
spring-projects:mainfrom
Develop-KIM:issue-2112
Open

Qualify child aggregate columns with the table they are selected from#2337
Develop-KIM wants to merge 1 commit into
spring-projects:mainfrom
Develop-KIM:issue-2112

Conversation

@Develop-KIM

Copy link
Copy Markdown
Contributor

Fixes #2112.

A Criteria on a property of a child aggregate rendered the column against the aggregate root's table, so Criteria.where("manual.content") on a LegoSet produced

FROM "LEGO_SET" LEFT OUTER JOIN "MANUAL" "manual" ON "manual"."LEGO_SET" = "LEGO_SET"."id1"
WHERE "LEGO_SET"."CONTENT" = ?

The join and the projection already use the child's aliased table; only the condition doesn't, so the statement asks for a column the root table does not have and fails with a BadSqlGrammarException (or silently matches nothing where the root happens to have a same-named column, as in the issue).

QueryMapper.mapCondition builds the column with table.column(...), where table is always the root table. The mapped column name already comes from the leaf property of the path, so the name was right and the qualification was wrong. This resolves the table from the property path instead, the same way SqlContext.getTable(AggregatePath) does for the projection: the table owner of the path, aliased, and the root table when the path has no table alias (a root property or an embedded one).

Sort had the same problem — Query.sort(Sort.by("manual.content")) rendered ORDER BY "LEGO_SET"."CONTENT" — so createSimpleOrderByFields goes through the same resolution.

One case needs to stay on the root table: an entity-valued property whose value is written to a column of the owning table by a custom converter. There the path's own table owner is the child's table, but the column belongs to the parent, so paths whose leaf is an entity keep the root table. That is what PartTreeJdbcQueryUnitTests.considersConvertersForQueryArguments (GH-2059) covers, and it stays green.

Tests: two unit tests in SqlGeneratorUnitTests for the rendered criteria and sort, and an integration test in AbstractJdbcAggregateTemplateIntegrationTests that queries LegoSet by manual.content — it fails with BadSqlGrammarException without the change. mvn verify on spring-data-jdbc is green (565 unit, 630 integration). I could not run -Pall-dbs here, so the integration tests ran on HSQLDB/H2 only; the change is dialect independent.

This was prepared with AI assistance (Claude Code) and reviewed by me before submitting.

A Criteria or Sort referring to a property of a child aggregate rendered the
column against the aggregate root's table, so the generated SQL asked for a
column the root table does not have. We now resolve the table from the
property path, which qualifies such a column with the joined child table.

Closes spring-projects#2112

Signed-off-by: Donghwan Kim <kimdonghwan913@gmail.com>
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Aug 6, 2026
@schauder schauder self-assigned this Aug 7, 2026
@schauder schauder added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Aug 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: enhancement A general enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The columns of the child aggregates are rendered incorrectly in the Criteria API

3 participants