Fix exists() cross-join caused by duplicate CriteriaQuery root#15419
Open
jamesfredley wants to merge 1 commit into7.0.xfrom
Open
Fix exists() cross-join caused by duplicate CriteriaQuery root#15419jamesfredley wants to merge 1 commit into7.0.xfrom
jamesfredley wants to merge 1 commit into7.0.xfrom
Conversation
AbstractHibernateGormStaticApi.exists() called criteriaQuery.from() twice, creating a second query root that produced a cartesian product. The generated SQL selected count(alias0) from Table alias1, Table alias0 where alias1.id=?, scanning the entire table for every matching row instead of a simple count. Reuse the existing queryRoot variable for the count select expression. Fixes #14334 Assisted-by: Claude Code <Claude@Claude.ai>
799f2f9 to
52f1180
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a Hibernate CriteriaQuery inefficiency in GormEntity.exists() where calling criteriaQuery.from() twice created duplicate roots and caused a cartesian product (cross-join), leading to full table scans.
Changes:
- Reuse the existing CriteriaQuery root in
AbstractHibernateGormStaticApi.exists()to avoid introducing a second root (and cross-join). - Add a core test (
ExistsCrossJoinSpec) that captures SQL viaStatementInspectorto assert no cross-join/comma-join is generated forexists(). - Add an integration test example (
ExistsSpec) ingrails-test-examples/gormvalidatingexists()behavior in a full Grails app context.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
grails-data-hibernate5/core/src/main/groovy/org/grails/orm/hibernate/AbstractHibernateGormStaticApi.groovy |
Fixes the duplicate-root CriteriaQuery construction by counting the existing queryRoot. |
grails-data-hibernate5/core/src/test/groovy/org/grails/orm/hibernate/ExistsCrossJoinSpec.groovy |
Adds regression coverage to ensure exists() does not generate cross-joins in SQL. |
grails-test-examples/gorm/src/integration-test/groovy/gorm/ExistsSpec.groovy |
Adds functional/integration validation of exists() in the sample app. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
jdaugherty
approved these changes
Feb 20, 2026
Contributor
jdaugherty
left a comment
There was a problem hiding this comment.
I'm assuming the test failure is due to test flakyness
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #14334
GormEntity.exists()was performing a cartesian product (cross-join) against the entire table due tocriteriaQuery.from()being called twice inAbstractHibernateGormStaticApi.exists().Reproducer: https://github.com/scottlollman/grails-data-mapping-issue-2071
Root Cause
AbstractHibernateGormStaticApi.exists()created two query roots:Each call to
criteriaQuery.from()adds a new root to the query. Two roots on the same table produce a cross-join:While the boolean result was technically correct (non-zero = true), the query performed a full table scan for every
exists()call.Fix
One-line change - reuse the existing
queryRootvariable instead of callingcriteriaQuery.from()a second time:Tests
Unit Tests (
ExistsCrossJoinSpec) - 4 testsIntegration tests using a standalone
HibernateDatastorewith H2:existsreturns true for existing entityexistsreturns false for non-existent idexistsdoes not produce a cross-join (SQL captured via HibernateStatementInspector, verified no cross-join or comma-join pattern)existswith multiple rows returns correct resultFunctional Tests (
ExistsSpec) - 3 testsFull Grails integration tests in
grails-test-examples/gormusing the existingProductdomain class:existsreturns true for persisted entityexistsreturns false for non-existent idexistsreturns correct result with multiple rows in tableExisting
ReadOperationSpectests continue to pass.