You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@grails.apache.org by "gustavoharff (via GitHub)" <gi...@apache.org> on 2026/10/11 02:12:03 UTC
[PR] Hibernate 7: keep HQL queries with a subquery intact [grails-core]
gustavoharff opened a new pull request, #16597:
URL: https://github.com/apache/grails-core/pull/16597
## Description
On Hibernate 7, an HQL query whose only `select` is in a subquery is rewritten into an invalid one before it reaches Hibernate:
```groovy
Book.findAll('from Book where title in (select name from Author where name like :p)', [p: 'A%'])
```
is sent as `select e.name from Author e where name like :p)` and fails with a `SyntaxException`. The same happens to an `update` or a `delete` with a subquery, and to a query with `select ` inside a string literal. An `insert ... select` loses its `insert into` and fails with `IllegalMutationQueryException: Expecting a mutation query`. Hibernate 7 itself accepts all of these queries, and they ran on Hibernate 5.
`HqlQueryContext` looked for the first `"select "` and `" from "` anywhere in the HQL. `normalizeNonAliasedSelect`, which gives an unaliased query the alias `e`, then rebuilt the query from that `select` and dropped what came before it. `countHqlProjections`, `getSingleProjectionClause` and `getRootAlias` read the same positions, so such a query also got `Object` instead of the entity as its result type.
Changes:
- `indexOfTopLevel` finds a keyword outside parentheses, string literals and quoted identifiers. The four methods use it for the `select` and `from` of the query, so those of a subquery or a literal are not taken for them.
- `normalizeNonAliasedSelect` keeps what precedes the `select`, such as the `insert into` of an insert-select.
## Testing
- `HibernateGormStaticApiSpec`, end to end through the static API: `findAll` and `executeQuery` with a subquery and no select clause, `executeUpdate` with an `update` and a `delete` with a subquery, an `insert ... select`, and a subquery in the select clause.
- `HqlQueryContextSpec`: new rows for `getTarget`, `normalizeNonAliasedSelect` and `countHqlProjections`, and a table for `indexOfTopLevel` (a subquery, a string literal with a parenthesis, the column list of an insert).
- 12 of the new cases fail on `8.0.x` without this change (9 in `HqlQueryContextSpec`, 3 in `HibernateGormStaticApiSpec`). The others cover the new helper, or guard queries that already worked, such as a subquery in the select clause.
- `./gradlew :grails-data-hibernate7-core:codeStyle`: no violations.
- All passing:
- `:grails-data-hibernate7-core:test`: 3725 tests, 0 failures
- `:grails-data-hibernate7:test`
- `:grails-data-hibernate7-spring-boot:test`
- `:grails-data-hibernate7-dbmigration:test`
- `:grails-data-hibernate7-dbmigration-core:test`
- `:grails-test-examples-hibernate7-criteria-extension:integrationTest`
- `:grails-test-examples-hibernate7-gorm:integrationTest`
- The full `./gradlew build --rerun-tasks` was not run; the change is confined to `grails-data-hibernate7`.
## Contributor Checklist
### Issue and Scope
- [ ] This PR is linked to an existing issue that has been **acknowledged or approved** by the project team. If no approved issue exists, please give background on why this change is necessary. Tickets are preferred for release change log history.
- [ ] This PR addresses the **complete scope** of the linked issue. Partial implementations or unfinished work should not be submitted for review.
- [x] This PR contains a **single, focused change**. Unrelated changes should be submitted as separate pull requests.
- [x] This PR targets the **correct branch** for the type of change:
- **Patch release branches** (e.g., `7.0.x`): Bug fixes only. No new features or API changes.
- **Minor release branches** (e.g., `7.1.x`): New features are welcome, but breaking existing APIs must be avoided.
- **Major release branches** (e.g., `8.0.x`): Reserved for major changes. Breaking API changes are permitted.
### Code Quality
- [x] I have **added or updated tests** that cover the changes introduced in this PR. All code contributions are expected to include appropriate test coverage.
- [ ] I have verified that all existing tests pass by running `./gradlew build --rerun-tasks`.
- [x] My code follows the project's **code style** guidelines. I have run `./gradlew codeStyle` and resolved any violations. See [Code Style](../CONTRIBUTING.md#code-style) for details.
- [x] This PR does **not** include mass reformatting, style-only changes, or large-scale refactoring unless it was **explicitly approved** in the linked issue. Unsolicited reformatting will not be accepted.
- [x] If generative AI tooling was used in preparing this contribution, a quality model was used to ensure contributions are **consistent with the project's quality standards**.
### Licensing and Attribution
- [x] All contributed code is provided under the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0), and new source files include the appropriate **Apache license header**.
- [x] I have the necessary rights to submit this contribution and confirm it is my own original work (see [Legal Notice](../CONTRIBUTING.md#i-want-to-contribute)).
- [x] If generative AI tooling was used in preparing this contribution, I have followed the [Apache Software Foundation's policy on generative tooling](https://www.apache.org/legal/generative-tooling.html) and have properly attributed its use.
### Documentation
- [ ] If this PR introduces user-facing changes, I have included or updated the relevant documentation.
- [ ] If this PR adds a new feature, I have updated the **What's New** section of the Grails Guide.
- [ ] If this PR introduces breaking changes or changes that require user action during an upgrade, I have updated the **Upgrade Notes** for the corresponding version in the Grails Guide.
- [x] The PR description clearly explains **what** was changed and **why**.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
--
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: notifications-unsubscribe@grails.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Hibernate 7: keep HQL queries with a subquery intact [grails-core]
Posted by "jdaugherty (via GitHub)" <gi...@apache.org>.
jdaugherty merged PR #16597:
URL: https://github.com/apache/grails-core/pull/16597
--
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: notifications-unsubscribe@grails.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Hibernate 7: keep HQL queries with a subquery intact [grails-core]
Posted by "codecov[bot] (via GitHub)" <gi...@apache.org>.
codecov[bot] commented on PR #16597:
URL: https://github.com/apache/grails-core/pull/16597#issuecomment-6112244962
## [Codecov](https://app.codecov.io/gh/apache/grails-core/pull/16597?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache) Report
:white_check_mark: All modified and coverable lines are covered by tests.
:white_check_mark: Project coverage is 59.5890%. Comparing base ([`bfa7093`](https://app.codecov.io/gh/apache/grails-core/commit/bfa709384d5e03de3a4bd77c3e12e9b80d37e4e2?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)) to head ([`f0f8729`](https://app.codecov.io/gh/apache/grails-core/commit/f0f8729b27cc6394829f45b3b89a9ab6651564f0?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)).
<details><summary>Additional details and impacted files</summary>
[](https://app.codecov.io/gh/apache/grails-core/pull/16597?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=apache)
```diff
@@ Coverage Diff @@
## 8.0.x #16597 +/- ##
=============================================
Coverage 59.5890% 59.5890%
- Complexity 25035 25036 +1
=============================================
Files 2175 2175
Lines 108651 108651
Branches 19821 19821
=============================================
Hits 64744 64744
Misses 34965 34965
Partials 8942 8942
```
</details>
<details><summary> :rocket: New features to boost your workflow: </summary>
- :snowflake: [Test Analytics](https://docs.codecov.com/docs/test-analytics): Detect flaky tests, report on failures, and find test suite problems.
- :package: [JS Bundle Analysis](https://docs.codecov.com/docs/javascript-bundle-analysis): Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
</details>
--
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: notifications-unsubscribe@grails.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Hibernate 7: keep HQL queries with a subquery intact [grails-core]
Posted by "testlens-app[bot] (via GitHub)" <gi...@apache.org>.
testlens-app[bot] commented on PR #16597:
URL: https://github.com/apache/grails-core/pull/16597#issuecomment-6113030461
## 🚨 All tests passed but jobs failed 🚨
### Failed Jobs without Test Failures
❌ [CI / Build Grails-Core \(Ubuntu JDK 21\)](https://github.com/apache/grails-core/actions/runs/38159281103/job/114528753163?pr=16597)
🏷️ Commit: f0f8729b27cc6394829f45b3b89a9ab6651564f0
▶️ Tests: 96445 executed
⚪️ Checks: 89/89 completed
### Rerun Controls
Click the checkbox to trigger a rerun:
- [ ] **Rerun jobs**
---
_Learn more about TestLens at [testlens.app/docs](https://testlens.app/docs/features/pr-comment/)._
--
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: notifications-unsubscribe@grails.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Hibernate 7: keep HQL queries with a subquery intact [grails-core]
Posted by "borinquenkid (via GitHub)" <gi...@apache.org>.
borinquenkid commented on PR #16597:
URL: https://github.com/apache/grails-core/pull/16597#issuecomment-6111641599
Pushed one commit on top of this branch (f0f8729b27). Thanks for the fix, the top-level `select`/`from` search is the right approach.
One gap I found: `indexOfTopLevel` matched the keyword anywhere, so a name that merely contains it (`preselect`, `autoselect`, `fromselect`) in a `where` clause was taken as the query's `select`, and the result type came back as `Object` instead of the entity. The keyword now has to start a word (not after an identifier character or `.`), via a small `startsWord` helper.
I added 5 cases to `HqlQueryContextSpec`: keyword inside a longer word, an escaped quote (`'it''s select x from y'`), a `with` clause before the select, `from X b select b.title`, and a root-alias projection with a subquery in the where. Only the first failed on the original code; the others document behaviour that already held. `HqlQueryContextSpec` (61) and `HibernateGormStaticApiSpec` (79) pass, and codenarcTest and checkstyleMain are clean.
Follow-up refactor (splitting `HqlQueryContext`) is tracked in #16616 and will land after the Java-to-Groovy conversion (#16572).
🤖 Generated with [Claude Code](https://claude.com/claude-code)
--
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: notifications-unsubscribe@grails.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org