gustavoharff opened a new issue, #16762:
URL: https://github.com/apache/grails-core/issues/16762

   ### Expected Behavior
   
   A subquery takes a path such as `owner.firstName` from its own root, 
whatever association blocks the outer query has:
   
   ```groovy
   Pet.createCriteria().list {
       owner {
           eq('firstName', 'Homer')
       }
       'in'('name', new DetachedCriteria(Pet).build {
           projections {
               property('owner.firstName')
           }
       })
   }
   ```
   
   should select the pets of Homer whose name is the first name of the owner of 
any pet.
   
   ### Actual Behaviour
   
   The subquery takes `owner` from the outer query and comes out correlated:
   
   ```sql
   ... from pet p1_0 join person o1_0 on o1_0.id=p1_0.owner_id
   where o1_0.first_name=?
     and p1_0.name in ((select o3_0.first_name from pet p2_0, person o3_0 where 
o3_0.id=p1_0.owner_id))
   ```
   
   so it only matches pets named after their own owner, and the result depends 
on whether the `owner { ... }` block comes before or after the subquery.
   
   The same query built with `new DetachedCriteria(Pet).build { ... }`, and a 
subquery inside a nested block, are not affected (covered by #16559).
   
   ### Cause
   
   In the `createCriteria` builder every association block records a join type 
(`CriteriaMethodInvoker` calls `hibernateQuery.join(name, joinType)`, `INNER` 
when none is given), and `JpaCriteriaQueryCreator.registerDetachedJoins` turns 
each recorded join type into an alias definition named after the path. A 
subquery context looks these up through its parent: `ExpressionResolver` sees 
`owner` as a defined alias (`AliasRegistry.isDefined` / `getDefinition` walk 
the parent registries) and then takes the outer join from the parent 
`JoinTracker`.
   
   A join type recorded for an association block is not an alias the user 
named, so it should not be visible to a subquery; only explicit aliases 
(`createAlias`, `as`) should correlate.
   
   ### Steps To Reproduce
   
   Run the query above on Hibernate 7 with a `Pet` that has an `owner` 
(`Person`) and a pet named after the first name of another pet's owner.
   
   ### Environment Information
   
   - Grails 8.0.x, `grails-data-hibernate7`
   - Reproduced on `8.0.x` at 956b3e719d, before and after #16559
   
   ### Example Application
   
   The query above, in the style of 
`HibernateCriteriaBuilderNestedJoinTypeSpec` in `grails-data-hibernate7/core`.
   
   ### Version
   
   8.0.x
   


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