Qualify child aggregate columns with the table they are selected from - #2337
Open
Develop-KIM wants to merge 1 commit into
Open
Qualify child aggregate columns with the table they are selected from#2337Develop-KIM wants to merge 1 commit into
Develop-KIM wants to merge 1 commit into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2112.
A
Criteriaon a property of a child aggregate rendered the column against the aggregate root's table, soCriteria.where("manual.content")on aLegoSetproducedThe 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.mapConditionbuilds the column withtable.column(...), wheretableis 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 waySqlContext.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).Sorthad the same problem —Query.sort(Sort.by("manual.content"))renderedORDER BY "LEGO_SET"."CONTENT"— socreateSimpleOrderByFieldsgoes 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
SqlGeneratorUnitTestsfor the rendered criteria and sort, and an integration test inAbstractJdbcAggregateTemplateIntegrationTeststhat queriesLegoSetbymanual.content— it fails withBadSqlGrammarExceptionwithout the change.mvn verifyonspring-data-jdbcis green (565 unit, 630 integration). I could not run-Pall-dbshere, 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.