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


##########
grails-data-hibernate7/core/src/main/groovy/org/grails/orm/hibernate/query/ExpressionResolver.java:
##########
@@ -101,25 +104,45 @@ private Expression<?> resolveFromAlias(String alias, 
String subPath) {
             return aliased; // It's a non-From expression (projection alias) 
or subPath is null
         }
 
-        // 2. Check if already joined in THIS or PARENT JoinTracker 
(correlated joins)
-        aliased = joinTracker.getJoin(alias);
-        if (aliased != null) {
-            aliasRegistry.realize(alias, aliased);
-            return subPath != null ? getPath((From<?, ?>) aliased, subPath) : 
aliased;
+        HibernateAlias def = aliasRegistry.getDefinition(alias);
+        if (def == null || def.path() == null) {
+            return null;
         }
-
-        // 3. If defined but not joined, materialize it on the CURRENT root
-        if (aliasRegistry.isDefined(alias)) {
-            HibernateAlias def = aliasRegistry.getDefinition(alias);
-            if (def != null && def.path() != null) {
-                aliased = joinTracker.getRoot().join(def.path(), 
def.joinType());
-                aliasRegistry.realize(alias, aliased);
-                joinTracker.addJoin(alias, (From<?, ?>) aliased);
-                return subPath != null ? getPath((From<?, ?>) aliased, 
subPath) : aliased;
+        if (!def.explicit()) {
+            aliased = resolveAssociation(def.path(), def.joinType());
+        } else {
+            Path<?> path = getPath(joinTracker.getRoot(), def.path());
+            boolean elementCollection = path != null && path.getModel() 
instanceof PluralAttribute<?, ?, ?> attribute &&
+                    attribute.getPersistentAttributeType() == 
PersistentAttributeType.ELEMENT_COLLECTION;
+            boolean sharesPath = !elementCollection && 
def.equals(aliasRegistry.getAssociationAlias(def.path()));
+            From<?, ?> join = sharesPath ? joinTracker.getOwnJoin(def.path()) 
: null;
+            if (join == null) {
+                join = joinTracker.getRoot().join(def.path(), def.joinType());
+                if (sharesPath) {
+                    joinTracker.addJoin(def.path(), join);
+                }
             }
+            aliased = join;
         }
+        aliasRegistry.realize(alias, aliased);
+        return subPath != null ? getPath((From<?, ?>) aliased, subPath) : 
aliased;
+    }
 
-        return null;
+    /**
+     * Resolves an association path independently of named aliases, even when 
an alias has the same name.
+     */
+    public From<?, ?> resolveAssociation(String path, JoinType joinType) {
+        From<?, ?> join = joinTracker.getOwnJoin(path);
+        if (join == null) {
+            HibernateAlias associationAlias = 
aliasRegistry.getAssociationAlias(path);

Review Comment:
   `createAlias('favorite', 'a', LEFT)` then `createAlias('favorite', 'b', 
INNER)`, with no restriction on `b`, materializes only the first LEFT join. The 
INNER join never reaches the query, so a root with no favorite is kept. Before 
this change the association used the last join type.
   
   Every explicitly requested alias has to be materialized, even when no 
restriction, projection, or order names it. Please cover that pair and assert 
the INNER join is present.



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