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


##########
grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/query/ExpressionResolver.java:
##########
@@ -112,7 +114,18 @@ 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());
+                Path<?> path = getPath(joinTracker.getRoot(), def.path());
+                boolean elementCollection = path != null && path.getModel() 
instanceof PluralAttribute<?, ?, ?> attribute &&
+                        attribute.getPersistentAttributeType() == 
PersistentAttributeType.ELEMENT_COLLECTION;
+                // Entity association criteria and their alias share a join. 
Element collection aliases
+                // are independent joins and must not replace the 
collection-valued path itself.
+                aliased = elementCollection ? null : 
joinTracker.getOwnJoin(def.path());

Review Comment:
   A locally declared alias still loses to an inherited join of the same name. 
`resolveFromAlias()` calls the parent-aware `getJoin(alias)` before this new 
`getOwnJoin(def.path())`. In the new exists test, change the inner alias from 
`inside` to `favorite` and restrict `favorite.label`. The expected row is still 
team A. This code resolves that predicate against the outer favorite, so the 
outer query requires both `first` and `second` and returns nothing.
   
   A local alias has to win over an inherited join of the association path. An 
explicit outer alias that the subquery is meant to see can stay on the parent 
lookup. Please add that collision as a regression, and cover two explicit 
aliases of one entity-valued collection.



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