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 7 commits into
7.2.xfrom
fix/detached-criteria-alias-chain-7.0.x
Open

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

Conversation

@borinquenkid

@borinquenkid borinquenkid commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Draft: do not merge. It changes queries that run today, so "no working code changes" below is wrong: see #17105 (comment). A workaround is on #14647.

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 (issue #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.

@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.

@borinquenkid
borinquenkid marked this pull request as draft November 26, 2026 00:59
@borinquenkid borinquenkid added no ci and removed run ci labels Nov 26, 2026
The 7.0.x patch line takes no more fixes after the 2026-11-25 releases;
this PR moves to 7.2.x. Its upgrade note moves from the 7.0.x section of
upgrading60x.adoc to a new Grails 7.2.6 section of upgrading72x.adoc.

Claude-Session: https://claude.ai/code/session_01Dmki4RzWwreEC2GkkyFnvL
@borinquenkid
borinquenkid changed the base branch from 7.0.x to 7.2.x November 26, 2026 02:47
@borinquenkid

Copy link
Copy Markdown
Member Author

Moved to 7.2.x: after the 2026-11-25 releases the 7.x line takes fixes on 7.2.x only. The branch has 7.2.x merged in and its module tests and codeStyle pass on it. The upgrade note moved from the 7.0.x section of upgrading60x.adoc to a new Grails 7.2.6 section of upgrading72x.adoc, and now says 7.2.6; the code is unchanged. A fresh approval on the new head is needed for the merge rule.

@borinquenkid
borinquenkid marked this pull request as ready for review November 26, 2026 02:47
@borinquenkid borinquenkid added run ci and removed no ci labels Nov 26, 2026
@borinquenkid
borinquenkid marked this pull request as draft November 26, 2026 09:06
@borinquenkid
borinquenkid marked this pull request as ready for review November 26, 2026 09:06
@borinquenkid
borinquenkid marked this pull request as draft December 1, 2026 12:38
@borinquenkid

Copy link
Copy Markdown
Member Author

@jamesfredley I have turned this into a draft; please do not merge it. The handling of aliases in blocks that I added during review changes queries that run today, so the description and the upgrade note are wrong where they say no working code changes. I ran these with Hibernate 5 on 7.2.x (43b9038) and on eab92a1:

Criteria on Parent (unless noted) 7.2.x This pull request
createAlias('children','c'); and { createAlias('children.related','r') }; eq('r.name','x') the parent whose own related is x the parent whose child's related is x
and { createAlias('children.related','r') }; eq('r.name','x') the parent whose own related is x SQLGrammarException
and { createAlias('parent.parent','gp') }; eq('gp.name','a') on a self-referencing class one row SQLGrammarException
children { or { createAlias('related.tag','t') } } one parent two parents

I also tried a smaller version on 7.0.x that changes only AbstractDetachedCriteria.createAlias. It is not safe either: in cases where 7.0.x throws, it returns wrong rows when the continued alias is used in a subquery or follows a full dotted path declared in a block, and it ignores the filter when the chain is declared inside or.

As far as I can tell, a correct fix needs Hibernate 5 to know which paths were continued from an alias, which is more change to code every datastore shares than I want to carry on a patch line. So I am not fixing #14647 on 7.x for now. There is a workaround on #14647, and the alias declared in an association block is #17248.

@testlens-app

testlens-app Bot commented Dec 6, 2026 •

Copy link
Copy Markdown

✅ All tests passed ✅

⚠️ TestLens detected flakiness ⚠️

Failed Jobs without Test Failures

❌ CI - Groovy Joint Validation Build / build_grails
❌ CI / Hibernate5 Functional Tests (Java 25, indy=false)

Test Summary

Tip

Inspect all details with the TestLens CLI: testlens pr 17105

CI - Groovy Joint Validation Build / build_grails > :grails-test-examples-mongodb-base:integrationTest

Test Runs Flakiness
OpenSessionInViewSpec > an action that goes asynchronous with #how closes its session as the request goes asynchronous > an action that goes asynchronous with AsyncController.startAsync() closes its session as the request goes asynchronous ❌ 🚫 0% 🟢

CI / Functional Tests (Java 17, indy=false) > :grails-test-examples-datasources:integrationTest

Test Runs Flakiness
OsivRequestSessionSpec > a task a request returns lazily loads associations of the entities its action loaded ❌ ✅ 0% 🟢

CI / Functional Tests (Java 25, indy=false) > :grails-test-examples-datasources:integrationTest

Test Runs Flakiness
OsivRequestSessionSpec > a task a request returns lazily loads associations of the entities its action loaded ❌ ✅ 0% 🟢

🏷️ Commit: eab92a1
▶️ Tests: 18771 executed
⚪️ Checks: 187/187 completed

Test Failures

OsivRequestSessionSpec > a task a request returns lazily loads associations of the entities its action loaded (:grails-test-examples-datasources:integrationTest in CI / Functional Tests (Java 17, indy=false) | Attempt 1/2)
Condition failed with Exception:

http('/requestSession/lazyVolumes?name=asynchronous').assertEquals(200, 'first,second')
|                                                     |
|                                                     org.opentest4j.AssertionFailedError: HTTP Status differs
|                                                     	at org.apache.grails.testing.http.client.TestHttpResponse.verify(TestHttpResponse.groovy:980)
|                                                     	at org.apache.grails.testing.http.client.TestHttpResponse.verifyStatus(TestHttpResponse.groovy:840)
|                                                     	at org.apache.grails.testing.http.client.TestHttpResponse.assertEquals(TestHttpResponse.groovy:580)
|                                                     	at functionaltests.OsivRequestSessionSpec.a task a request returns lazily loads associations of the entities its action loaded(OsivRequestSessionSpec.groovy:109)
<org.apache.grails.testing.http.client.TestHttpResponse@144a750f delegate=(GET http://localhost:33185/requestSession/lazyVolumes?name=asynchronous) 500 jsonSlurperConfig=org.apache.grails.testing.http.client.utils.JsonUtils$SlurperConfig(null, null, null, null, null) xmlSlurperConfig=org.apache.grails.testing.http.client.utils.XmlUtils$SlurperConfig@2034e91c>

	at functionaltests.OsivRequestSessionSpec.a task a request returns lazily loads associations of the entities its action loaded(OsivRequestSessionSpec.groovy:109)
Caused by: org.opentest4j.AssertionFailedError: HTTP Status differs
	at org.apache.grails.testing.http.client.TestHttpResponse.verify(TestHttpResponse.groovy:980)
	at org.apache.grails.testing.http.client.TestHttpResponse.verifyStatus(TestHttpResponse.groovy:840)
	at org.apache.grails.testing.http.client.TestHttpResponse.assertEquals(TestHttpResponse.groovy:580)
	... 1 more
OsivRequestSessionSpec > a task a request returns lazily loads associations of the entities its action loaded (:grails-test-examples-datasources:integrationTest in CI / Functional Tests (Java 25, indy=false) | Attempt 1/2)
Condition failed with Exception:

http('/requestSession/lazyVolumes?name=asynchronous').assertEquals(200, 'first,second')
|                                                     |
|                                                     org.opentest4j.AssertionFailedError: HTTP Status differs
|                                                     	at org.apache.grails.testing.http.client.TestHttpResponse.verify(TestHttpResponse.groovy:980)
|                                                     	at org.apache.grails.testing.http.client.TestHttpResponse.verifyStatus(TestHttpResponse.groovy:840)
|                                                     	at org.apache.grails.testing.http.client.TestHttpResponse.assertEquals(TestHttpResponse.groovy:580)
|                                                     	at functionaltests.OsivRequestSessionSpec.a task a request returns lazily loads associations of the entities its action loaded(OsivRequestSessionSpec.groovy:109)
<org.apache.grails.testing.http.client.TestHttpResponse@53a5b50f delegate=(GET http://localhost:37343/requestSession/lazyVolumes?name=asynchronous) 500 jsonSlurperConfig=org.apache.grails.testing.http.client.utils.JsonUtils$SlurperConfig(null, null, null, null, null) xmlSlurperConfig=org.apache.grails.testing.http.client.utils.XmlUtils$SlurperConfig@7b21879f>

	at functionaltests.OsivRequestSessionSpec.a task a request returns lazily loads associations of the entities its action loaded(OsivRequestSessionSpec.groovy:109)
Caused by: org.opentest4j.AssertionFailedError: HTTP Status differs
	at org.apache.grails.testing.http.client.TestHttpResponse.verify(TestHttpResponse.groovy:980)
	at org.apache.grails.testing.http.client.TestHttpResponse.verifyStatus(TestHttpResponse.groovy:840)
	at org.apache.grails.testing.http.client.TestHttpResponse.assertEquals(TestHttpResponse.groovy:580)
	... 1 more
OpenSessionInViewSpec > an action that goes asynchronous with #how closes its session as the request goes asynchronous > an action that goes asynchronous with AsyncController.startAsync() closes its session as the request goes asynchronous (:grails-test-examples-mongodb-base:integrationTest in CI - Groovy Joint Validation Build / build_grails | Attempt 1/2)
Condition not satisfied after 10.00 seconds and 116 attempts
	at spock.util.concurrent.PollingConditions.within(PollingConditions.java:185)
	at spock.util.concurrent.PollingConditions.eventually(PollingConditions.java:140)
	at functional.tests.OpenSessionInViewSpec.an action that goes asynchronous with #how closes its session as the request goes asynchronous(OpenSessionInViewSpec.groovy:125)
Caused by: Condition not satisfied:

RequestSessions.asyncSession != null
|               |            |
|               null         false
class functional.tests.RequestSessions

	at functional.tests.OpenSessionInViewSpec.an action that goes asynchronous with #how closes its session as the request goes asynchronous_closure8(OpenSessionInViewSpec.groovy:126)
	at functional.tests.OpenSessionInViewSpec.an action that goes asynchronous with #how closes its session as the request goes asynchronous_closure8(OpenSessionInViewSpec.groovy)
	at groovy.lang.Closure.call(Closure.java:433)
	at spock.util.concurrent.PollingConditions.within(PollingConditions.java:168)
	... 2 more

Rerun Controls

Click the checkbox to trigger a rerun:

  • Rerun jobs

Learn more about TestLens at testlens.app/docs.

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