borinquenkid opened a new pull request, #16773:
URL: https://github.com/apache/grails-core/pull/16773

   Fixes #16762
   
   ### Problem
   In Hibernate 7 criteria queries, top-level association blocks (such as 
`owner { eq('firstName', 'Ned') }`) and un-aliased association joins record the 
join path in `detachedCriteria.getJoinTypes()`. During JPA criteria 
translation, `JpaCriteriaQueryCreator` registered these join paths as aliases 
in `AliasRegistry` and tracked the materialized joins in `JoinTracker`. When an 
inner subquery was created via `JpaQueryContext.forSubquery(parentContext, 
...)` and projected a property through an association (such as 
`property('owner.firstName')`), the subquery context delegated to the parent 
query's `AliasRegistry` and `JoinTracker`, resolving the projection path 
against the parent query's physical join rather than creating an independent 
join on the subquery's root entity. This caused the subquery to unintentionally 
correlate with the outer query.
   
   Additionally, when an explicit alias was declared on an outer query and 
referenced inside a subquery, `ExpressionResolver` was attempting to 
materialize the join on the subquery's root rather than delegating 
materialization to the parent context that owns the alias.
   
   ### Solution
   - Differentiate explicit user-defined aliases from internal implicit joins 
by adding an `explicit` flag to 
[`HibernateAlias`](file:///Users/walterduquedeestrada/IdeaProjects/grails-core/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/query/HibernateAlias.java).
   - Restrict 
[`AliasRegistry`](file:///Users/walterduquedeestrada/IdeaProjects/grails-core/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/query/AliasRegistry.java)
 parent delegation (`isDefined`, `getDefinition`, `getRealized`, `hasRealized`) 
so that child subquery contexts only inherit explicit aliases from the parent 
query.
   - Update 
[`ExpressionResolver`](file:///Users/walterduquedeestrada/IdeaProjects/grails-core/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/query/ExpressionResolver.java)
 to resolve and materialize parent-defined explicit aliases on the parent 
context that owns them.
   - Isolate the physical join tracking of subqueries in 
[`JpaQueryContext`](file:///Users/walterduquedeestrada/IdeaProjects/grails-core/grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/query/JpaQueryContext.java)
 so that subquery contexts do not delegate physical joins to the parent's 
`JoinTracker`.
   - Add test coverage in 
[`SubqueryAssociationCorrelationSpec`](file:///Users/walterduquedeestrada/IdeaProjects/grails-core/grails-data-hibernate7/core/src/test/groovy/grails/orm/SubqueryAssociationCorrelationSpec.groovy)
 covering projections, criteria, nested association blocks, and correlated 
outer aliases, as well as unit tests in 
[`AliasRegistrySpec`](file:///Users/walterduquedeestrada/IdeaProjects/grails-core/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/query/AliasRegistrySpec.groovy)
 and 
[`JpaQueryContextSpec`](file:///Users/walterduquedeestrada/IdeaProjects/grails-core/grails-data-hibernate7/core/src/test/groovy/org/grails/orm/hibernate/query/JpaQueryContextSpec.groovy).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to