Skip to content

fix(datamapping): a createAlias path that starts with an alias declared earlier continues the path of that alias - #17105

Open
borinquenkid wants to merge 6 commits into
7.0.xfrom
fix/detached-criteria-alias-chain-7.0.x
Open

borinquenkid wants to merge 6 commits into
7.0.xfrom
fix/detached-criteria-alias-chain-7.0.x

Conversation

@borinquenkid

Copy link
Copy Markdown
Member

A path given to createAlias on a DetachedCriteria or a where query that starts with an alias declared earlier now continues the path of that alias: after createAlias('children', 'c'), the path c.related is children.related. Before, such a call failed with a QueryException or with invalid SQL, so no working code changes (Fixes #14647).

This is the 7.0.x part of #16890, split out as matrei asked on that pull request: a bug fix with no new API, in AbstractDetachedCriteria only. The rest of #16890 (the MultipleAliasesMappingContext opt-in and the Hibernate 5 builder changes) is not here.

  • DetachedCriteriaAliasChainSpec: four features; the two that join from an earlier alias fail on 7.0.x without the change.
  • grails-datamapping-core (252 tests) and grails-data-hibernate5-core (691 tests) pass, and codeStyle is clean.
  • The upgrade note is in the 7.0.18 section of upgrading60x.adoc; a call that failed before now works, and a first segment that is both an alias and a property names the alias.

https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested changes. An alias declared inside a block is not seen by the next alias in that block.

return associationPath
}
String head = associationPath.substring(0, dot)
for (Query.Criterion criterion in criteria) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alias named c is declared for children, then c.related is declared, both inside the same and. The second call starts from the root and throws, because c was added to the and and this only looks at the top list. Continue from c.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jamesfredley: confirmed. An alias declared inside and, or or not is not seen by a later alias that continues it, and the cause is wider than the lookup: on Hibernate 5 an association criterion with a dotted path inside a block is translated against the root, so fixing the lookup only moves the failure from createAlias to the query (all six block shapes fail that way). Fixing it properly changes how H5 and AbstractDetachedCriteria track aliases, which is too large for a 7.0.x patch release.

Workaround: declare the aliases at the top level and use them inside the block. That works and is covered by the specs in this PR, which is the chain #14647 reported.

The lookup fix itself works on H7 for all eight block shapes (top level, and, or, not, nested, split, outer first, full paths), so it goes to 8.1.x, with the split of the class in #17109.

https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL

@borinquenkid

Copy link
Copy Markdown
Member Author

James, the alias declared inside a block is a refusal now, as 7.0.x has no room for the full lookup fix. A path that continues an alias declared inside and, or, not or a nested block throws an IllegalArgumentException that says to declare the alias at the top level and use it inside the block. A spec pins the four shapes, and one more pins the workaround: the alias declared at the top level, used inside an or. The guide says which form works. The lookup itself goes to 8.1.x with #17109. Merged 7.0.x too.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested changes. An alias named like a property of the root is still not continued inside a block.

// a block that is still open is not in the criteria yet
List<Query.Criterion> inBlocks = new ArrayList<Query.Criterion>(criteria)
inBlocks.addAll(junctions)
if (declaredInBlock(inBlocks, head) && persistentEntity.getPropertyByName(head) == null) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alias named name is declared for related, then name.tag is declared, both inside the same and. The root also has a property named name. The second call starts from that property and throws. Continue from the alias.

…de a block also where the class has a property of that name

The refusal was skipped when the class had a property named like the alias, so the path started from that property and
failed with 'is not an association'. The alias names the first segment, as it does at the top level, so the refusal applies.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
@borinquenkid

Copy link
Copy Markdown
Member Author

@jamesfredley Fixed. The refusal was skipped when the class had a property named like the alias, so name.related started from the property name and failed with "is not an association". The alias names the first segment, as it does at the top level, so the refusal now applies in that case too, in and, or and a nested block. DetachedCriteriaAliasChainSpec has the three rows; they fail without the change. The upgrade note says so. grails-datamapping-core (252 tests) and the Hibernate 5 module (703) pass, and so does codeStyle.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Requested changes. An alias declared inside a block is still refused when it does not collide with a root property.

// the alias names the first segment, also where the class has a property of the same name, and it cannot be
// continued from here: the path would start from the root and fail further on, or find the wrong association,
// so say what to do
throw new IllegalArgumentException("The alias [$head] is declared inside a block (and, or, not), and the path [$associationPath] cannot continue from it there. Declare the alias at the top level of the query and use it inside the block.")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An alias named c is declared for children, then c.related is declared, both inside the same and. The root has no property named c. The second call is refused and told to declare c at the top. Continue from c.

…r it was declared, and declared by its path inside a block

An alias declared inside and, or or not was refused when a path continued it, although the alias belongs to the whole query. It is found in
the closed blocks and in the block that is still open now. Hibernate 5 then declared such an alias inside a junction by the name of its
association alone, so createAlias('c.related', 'r') looked for related on the root; it is declared by the path from the root, as it is outside
a block. DetachedCriteriaAliasChainSpec has the table of declaration sites for a detached criteria and for the criteria builder.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
@borinquenkid

Copy link
Copy Markdown
Member Author

@jamesfredley Fixed, and the refusal is gone: an alias belongs to the whole query, so a path that continues it is found wherever the alias was declared. Behind the refusal there was a second bug that your example only reaches once the first is out of the way: Hibernate 5 declared an alias inside and, or or not by the name of its association alone, so createAlias('c.related', 'r') looked for related on the root. It is declared by its path from the root now, as it is outside a block.

The table in DetachedCriteriaAliasChainSpec runs each row through a DetachedCriteria and through the criteria builder (withCriteria), 22 features in all:

  • Where the alias is declared: inside an and, inside an or, inside a nested block, in one block and continued in another, and with a name that is a property of the class.
  • How it is continued: once, twice (c, c.related, r.tag), and used inside the block it was declared in or after it.
  • Still refused, whether in a block or not: a path that continues past a property that is not an association, and a path that starts with a name no alias of the query declares.

Not covered, with the reason: a second alias for the same collection is not a separate join on Hibernate 5 (it keeps the latest alias of a path, as it always has; Hibernate 7 on 8.0.x joins each one), and order and projections over an alias declared in a block behave as they do for one declared at the top level, which the existing features pin. The note under 7.0.18 is rewritten, and the Hibernate 5, datamapping and datastore-core modules and codeStyle pass.

@testlens-app

testlens-app Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

🚨 All tests passed but jobs failed 🚨

Failed Jobs without Test Failures

❌ CI / Build Grails-Core (macos-latest, 21, true)

🏷️ Commit: 36d6b4f
▶️ Tests: 52667 executed
⚪️ Checks: 58/58 completed

Rerun Controls

Click the checkbox to trigger a rerun:

  • Rerun jobs

Learn more about TestLens at testlens.app/docs.

@jamesfredley jamesfredley left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The items this review found have been addressed. An alias declared inside a block is continued, and a colliding name is refused.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

3 participants