jamesfredley commented on code in PR #16573:
URL: https://github.com/apache/grails-core/pull/16573#discussion_r4338701789


##########
grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/query/ExpressionResolver.java:
##########
@@ -112,7 +112,13 @@ private Expression<?> resolveFromAlias(String alias, 
String subPath) {
         if (aliasRegistry.isDefined(alias)) {
             HibernateAlias def = aliasRegistry.getDefinition(alias);
             if (def != null && def.path() != null) {
-                aliased = joinTracker.getRoot().join(def.path(), 
def.joinType());
+                // The association criteria and its alias must refer to the 
same physical join,
+                // regardless of whether a predicate, projection or order 
resolves it first.
+                aliased = joinTracker.getJoin(def.path());
+                if (aliased == null) {
+                    aliased = joinTracker.getRoot().join(def.path(), 
def.joinType());
+                    joinTracker.addJoin(def.path(), (From<?, ?>) aliased);

Review Comment:
   Recording the element join under `def.path()` treats every later use of that 
path as the same join.
   
   `HibernateCriteriaBuilder.createAlias()` allows two explicit aliases of one 
basic collection (`schools: Set<String>`: `createAlias('schools', 'm'); 
createAlias('schools', 'h'); inList('m', ['MIT']); inList('h', ['Harvard'])`). 
Those must be independent element joins so a student at both schools still 
matches. The second alias hits `getJoin('schools')` and is forced onto the 
first join, so one element has to equal both values and the query returns 
nothing. The second alias's join type is also ignored.
   
   The same cache breaks collection-valued resolution. `createAlias('schools', 
's'); eq('s', 'MIT'); sizeEq('schools', 2)` used to resolve `schools` as the 
collection on the root. `resolve()` now finds the element join stored under 
`schools` and `criteriaBuilder.size()` receives a non-collection expression.
   
   Deduplicate the association join that one alias represents. Do not merge 
distinct explicit aliases, and do not let an element join satisfy a later 
lookup of the collection path itself. Please cover both with public criteria 
tests.



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