Thanks to visit codestin.com
Credit goes to github.com

Skip to content

fix(support): use JDSL model for count query to bypass strict JPQL parsers#1040

Merged
shouwn merged 4 commits intoline:developfrom
cj848:fix/EnhancedTypedQuery-count
Feb 26, 2026
Merged

fix(support): use JDSL model for count query to bypass strict JPQL parsers#1040
shouwn merged 4 commits intoline:developfrom
cj848:fix/EnhancedTypedQuery-count

Conversation

@cj848
Copy link
Collaborator

@cj848 cj848 commented Feb 25, 2026

Motivation

  • Fix a BadJpqlGrammarException that occurs in Spring Boot 4 (Spring Data JPA 3.4+) environments when using findPage with certain valid JPQL constructs.
  • Spring Data JPA 3.4 introduced a strict ANTLR-based JPQL parser for query enhancement, which rejects keywords like NULL within SELECT NEW constructor expressions.
  • Provide a robust, provider-agnostic way to generate count queries by leveraging Kotlin JDSL's internal Query Model instead of fragile string-based re-parsing.

Modifications

  • Model-based Count Query Generation:
    • Added SelectQueries.toCountQuery() utility in the query-model-jpql module. This allows safe transformation of a SelectQuery into a COUNT(1) query at the model level.
    • Updated JpqlEntityManagerUtils.kt in both support/spring-data-jpa and support/spring-data-jpa-boot4 modules to use this model-based approach for generating count queries.
  • Visibility and Compatibility Fixes:
    • Resolved Kotlin visibility-related copy() warnings (KT-11914) by encapsulating the transformation logic within the same module as the internal constructors.
    • Ensured the solution remains vendor-neutral, working seamlessly across Hibernate, EclipseLink, and other JPA providers without provider-specific code.
  • Validation:
    • Added Issue1039Test.kt in both example/spring-data-jpa and example/spring-data-jpa-boot4 to reproduce the reported issue and verify the fix.
    • Updated JpqlEntityManagerUtilsTest.kt to cover the new model-based execution path.

Result

  • Kotlin JDSL now supports pagination (findPage) for queries containing NULL literals or other complex expressions in SELECT NEW clauses on Spring Boot 4.
  • Pagination logic is now more efficient and reliable by avoiding redundant string-based parsing during query enhancement.
  • Consistent behavior across all Spring Data JPA support modules (Boot 2/3 and Boot 4).

Closes

#1039

--- korean

Motivation

  • Spring Boot 4 (Spring Data JPA 3.4+) 환경에서 findPage 호출 시 일부 유효한 JPQL 구문에 대해 발생하는 BadJpqlGrammarException을 해결합니다.
  • Spring Data JPA 3.4부터 도입된 엄격한 ANTLR 기반 JPQL 파서가 SELECT NEW 생성자 내의 NULL 키워드 등을 거부하는 문제를 해결하기 위함입니다.
  • 문자열 재파싱 방식(QueryEnhancer) 대신 Kotlin JDSL의 쿼리 모델(Query Model) 을 직접 조작하여 카운트 쿼리를 생성함으로써 근본적이고 프로바이더 중립적인 해결책을 제공합니다.

Modifications

  • 모델 기반 카운트 쿼리 생성:
    • query-model-jpql 모듈에 SelectQueries.toCountQuery() 유틸리티를 추가하여 모델 수준에서 SelectQueryCOUNT(1) 쿼리로 안전하게 변환하도록 했습니다.
    • support/spring-data-jpasupport/spring-data-jpa-boot4 모듈의 JpqlEntityManagerUtils.kt가 문자열 파서 대신 이 모델 기반 방식을 사용하도록 수정했습니다.
  • 가시성 및 호환성 개선:
    • internal 생성자 접근으로 인한 Kotlin copy() 경고(KT-11914)를 변환 로직의 모듈 내 캡슐화를 통해 해결했습니다.
    • 특정 JPA 벤더 전용 코드를 사용하지 않고 표준 JDSL 모델만 사용하여 벤더 중립성을 유지했습니다.
  • 검증:
    • example/spring-data-jpaexample/spring-data-jpa-boot4Issue1039Test.kt를 추가하여 문제를 재현하고 해결 여부를 검증했습니다.
    • JpqlEntityManagerUtilsTest.kt를 업데이트하여 모델 기반 카운트 쿼리 생성 경로를 테스트하도록 보강했습니다.

Result

  • Spring Boot 4 환경에서도 SELECT NEW 절에 NULL 리터럴 등이 포함된 쿼리에 대해 페이지네이션(findPage) 기능을 정상적으로 사용할 수 있습니다.
  • 쿼리 강화 과정에서 불필요한 문자열 파싱을 생략하여 페이지네이션 로직의 효율성과 안정성이 향상되었습니다.
  • 모든 Spring Data JPA 지원 모듈(Boot 2/3 및 Boot 4)에서 일관된 동작을 보장합니다.

Closes

#1039

@cj848 cj848 force-pushed the fix/EnhancedTypedQuery-count branch 6 times, most recently from 298a721 to 2f6e71d Compare February 25, 2026 05:32
@codecov-commenter
Copy link

codecov-commenter commented Feb 25, 2026

Codecov Report

❌ Patch coverage is 94.87179% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.76%. Comparing base (4ec574b) to head (42ad1dc).
⚠️ Report is 2 commits behind head on develop.

Files with missing lines Patch % Lines
...kotlinjdsl/querymodel/jpql/select/SelectQueries.kt 92.59% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #1040      +/-   ##
===========================================
+ Coverage    90.51%   90.76%   +0.24%     
===========================================
  Files          360      360              
  Lines         3805     3842      +37     
  Branches       231      239       +8     
===========================================
+ Hits          3444     3487      +43     
+ Misses         294      287       -7     
- Partials        67       68       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@cj848 cj848 changed the title fix(support): use JDSL model for count query to bypass strict JPQL pa… fix(support): use JDSL model for count query to bypass strict JPQL parsers Feb 25, 2026
@cj848 cj848 force-pushed the fix/EnhancedTypedQuery-count branch from 2f6e71d to acea337 Compare February 25, 2026 07:22
@cj848 cj848 requested a review from Copilot February 25, 2026 09:52
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a BadJpqlGrammarException that occurs in Spring Boot 4 (Spring Data JPA 3.4+) environments when using pagination with queries containing NULL literals or complex expressions in SELECT NEW clauses. The fix replaces fragile string-based count query generation with a robust model-based approach using Kotlin JDSL's internal Query Model.

Changes:

  • Added SelectQueries.toCountQuery() utility to transform SelectQuery into COUNT(1) query at the model level
  • Updated JpqlEntityManagerUtils in both spring-data-jpa and spring-data-jpa-boot4 modules to use model-based count query generation
  • Added integration tests (Issue1039Test.kt) in example projects to verify the fix
  • Enhanced unit tests to cover both model-based and fallback count query paths

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
query-model/jpql/src/main/kotlin/com/linecorp/kotlinjdsl/querymodel/jpql/select/SelectQueries.kt Added toCountQuery() factory method to transform SelectQuery into COUNT query at model level
support/spring-data-jpa/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/JpqlEntityManagerUtils.kt Replaced string-based count query with model-based approach, added fallback for non-select queries
support/spring-data-jpa-boot4/src/main/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/JpqlEntityManagerUtils.kt Applied same model-based count query logic for Boot4 compatibility
support/spring-data-jpa/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/JpqlEntityManagerUtilsTest.kt Added @OptIn for Internal APIs, split tests to cover model-based and fallback paths
support/spring-data-jpa-boot4/src/test/kotlin/com/linecorp/kotlinjdsl/support/spring/data/jpa/JpqlEntityManagerUtilsTest.kt Enhanced tests to cover both query generation paths
support/spring-data-jpa-boot4/src/main/kotlin/org/springframework/data/jpa/repository/query/QueryEnhancerFactoryAdaptor.kt Removed redundant import declarations
example/spring-data-jpa/src/test/kotlin/com/linecorp/kotlinjdsl/example/spring/data/jpa/jpql/select/Issue1039Test.kt Added integration test reproducing and verifying the fix for Issue #1039
example/spring-data-jpa-boot4/src/test/kotlin/com/linecorp/kotlinjdsl/example/spring/data/jpa/jpql/select/Issue1039Test.kt Added Boot4-specific integration test for the issue

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@cj848 cj848 force-pushed the fix/EnhancedTypedQuery-count branch 2 times, most recently from 1be56c0 to 2701f64 Compare February 25, 2026 11:23
@cj848 cj848 requested a review from Copilot February 25, 2026 11:23
@cj848 cj848 force-pushed the fix/EnhancedTypedQuery-count branch from 2701f64 to a5a0a4c Compare February 25, 2026 11:28
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 11 out of 11 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 11 out of 11 changed files in this pull request and generated 3 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 11 out of 11 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@cj848 cj848 force-pushed the fix/EnhancedTypedQuery-count branch 3 times, most recently from af889c4 to 6f189ac Compare February 25, 2026 22:23
@cj848 cj848 requested a review from Copilot February 25, 2026 22:23
@cj848 cj848 force-pushed the fix/EnhancedTypedQuery-count branch from 6f189ac to a953917 Compare February 25, 2026 22:28
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 11 out of 11 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@cj848 cj848 force-pushed the fix/EnhancedTypedQuery-count branch from a953917 to 292ee0f Compare February 25, 2026 23:12
- Use COUNT(1) for non-distinct queries for accuracy and compatibility.
- Reuse QueryEnhancer in JpqlEntityManagerUtils to avoid redundant parsing.
- Strip FETCH attribute from joins in count queries to prevent JPA errors.
- Refine KDoc for toCountQuery and add warnings for groupBy/having results.
- Update tests to verify clause preservation and COUNT(1) behavior.
@cj848 cj848 force-pushed the fix/EnhancedTypedQuery-count branch from 292ee0f to 12d2293 Compare February 25, 2026 23:42
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 12 out of 12 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@shouwn shouwn merged commit d719d97 into line:develop Feb 26, 2026
8 checks passed
@cj848 cj848 deleted the fix/EnhancedTypedQuery-count branch February 27, 2026 01:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants