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

   ## Description
   
   Hibernate 7 queries are typed, so the HQL methods of GORM for Hibernate 7 
choose a result type for each query from its select clause 
(`HqlQueryContext.getTarget`). Two cases chose the wrong one.
   
   ### A single select item other than the root alias
   
   For a select clause with one item that has no `.` and is not an aggregate, 
the query was run with the queried entity's class as the result type. That is 
right for `select b from Book b`, but not for a joined alias or a function:
   
   ```groovy
   Book.executeQuery('select distinct a from Book b join b.author a')
   // QueryTypeMismatchException: Incorrect query result type: query produces 
'Author' but type 'Book' was given
   ```
   
   The entity class is now used only when the item is the root alias, which 
includes the entity name in an unaliased query. Any other single item, such as 
a joined alias, `current_date` or `new map(...)` without a qualified property, 
uses `Object`, so Hibernate returns the value as it is.
   
   ### Native queries without an alias
   
   `findWithSql` and `findAllWithSql` run native SQL and return entities. When 
the SQL had no table alias, its select clause was normalized from `*` to `e.*`. 
The `.` then made the query run as a scalar query, so `findAllWithSql('select * 
from book order by title')` returned rows of column values instead of `Book` 
instances.
   
   A native query now always returns the entity, as GORM for Hibernate 5 does 
by adding the entity to every native query.
   
   ### Tests
   
   - New `HqlSelectResultTypeSpec` exercises the public API:
     - `executeQuery` with a joined alias, with and without parameters, returns 
the joined entities
     - selecting the root alias, a property or an association still returns 
entities or values
     - `findAllWithSql` with unaliased `select *` returns entities
   
     Without the change, the joined-alias and native cases fail.
   - `HqlQueryContextSpec`:
     - `getTarget` cases for the root alias (aliased, with `as`, unaliased, 
with joins), a joined alias, a theta join, `current_date` and `new map(...)`
     - a native-query case for `prepare`
     - the `isPropertyProjection` feature is replaced by these cases, since the 
method is gone
   
   All tests of `:grails-data-hibernate7-core:test` pass, and so do 
`checkstyleMain` and `codenarcMain`.
   


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