Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace derived CriteriaQuery with String-based queries #3588

Closed
2 of 5 tasks
mp911de opened this issue Aug 23, 2024 · 5 comments
Closed
2 of 5 tasks

Replace derived CriteriaQuery with String-based queries #3588

mp911de opened this issue Aug 23, 2024 · 5 comments
Assignees
Labels
type: enhancement A general enhancement

Comments

@mp911de
Copy link
Member

mp911de commented Aug 23, 2024

We should explore how we could switch from Criteria Queries to JPQL queries as Hibernate has a much better efficiency running String-based queries:

CriteriaBuilder query
Benchmark                                   Mode  Cnt       Score      Error  Units
RepositoryFinderTests.derivedFinderMethod  thrpt    5  110633,482 ± 3980,501  ops/s

String-based query
Benchmark                                   Mode  Cnt       Score       Error  Units
RepositoryFinderTests.derivedFinderMethod  thrpt    5  344518,379 ± 24561,002  ops/s

We would need to build our own JpqlQueryBuilder that mimics CriteriaBuilder in terms of expressions, predicates and automated inclusion of joins.

Collection of related tickets:

@mp911de mp911de added the type: enhancement A general enhancement label Aug 23, 2024
@mp911de
Copy link
Member Author

mp911de commented Aug 23, 2024

It is possible to use String-based queries. We should consider how to inject Keyset-pagination predicates and introduce joins for sort by fields that work for part tree queries and JPQL queries using ideally the same mechanism.

@derekm
Copy link

derekm commented Aug 27, 2024

Is this why FilterDef broke somewhere between Spring Boot 2.1 and 2.5? See #2565

If queries are rendered to JPQL/HQL again, then can they be created through EntityManager, so that those of us who unwrap Session and enable filters will be able to use @FilterDefs again?

@mp911de
Copy link
Member Author

mp911de commented Aug 28, 2024

If queries are rendered to JPQL/HQL again,

We've been using CriteriaQuery for derived queries and SimpleJpaRepository implementations since the inception of Spring Data JPA.

@quaff
Copy link
Contributor

quaff commented Sep 4, 2024

Why does JPQL queries perform better than Criteria Queries? Parsing JPQL into SQM seems more heavy than mapping CriteriaQuery to SQM, maybe it should be improved at Hibernate side, or something going wrong at Spring Data JPA side.

@mp911de
Copy link
Member Author

mp911de commented Sep 4, 2024

Why does JPQL queries perform better than Criteria Queries?

Because Hibernate seems to cache the parsed HQL query.

@mp911de mp911de linked a pull request Oct 28, 2024 that will close this issue
@mp911de mp911de changed the title Explore using JPQL queries instead of Criteria Queries Migrate off Criteria Queries to JPQL queries Nov 21, 2024
@mp911de mp911de added this to the 4.0 M1 (2025.1.0) milestone Nov 21, 2024
@mp911de mp911de changed the title Migrate off Criteria Queries to JPQL queries Replace derived CriteriaQuery with String-based queries Nov 22, 2024
mp911de added a commit that referenced this issue Nov 22, 2024
Introduce new DSL to construct JPQL queries. Refactor ParameterMetadata to PartTreeParameterBinding.

Disable Keyset pagination with projections for Eclipselink as Eclipselink doesn't consider type hints for JPQL queries.

Closes #3588
Original pull request: #3653
mp911de pushed a commit that referenced this issue Nov 22, 2024
Make usage of ParameterExpression more explicit. Add JPQL rendering tests.
Favor Metamodel over From for building jpql queries.
Align IsNull and IsNotNull handling.
Support Derived Delete and Exists, consider null values when caching queries.

See #3588
Original pull request: #3653
mp911de added a commit that referenced this issue Nov 22, 2024
Remove method overloads accepting pure strings. Use switch-expressions. Correctly navigate nested joins. Introduce PathExpression interface, refine naming.

See #3588
Original pull request: #3653
mp911de added a commit that referenced this issue Nov 22, 2024
See #3588
Original pull request: #3653
@mp911de mp911de closed this as completed Nov 22, 2024
mp911de added a commit that referenced this issue Nov 22, 2024
See #3588
Original pull request: #3653
mp911de added a commit that referenced this issue Nov 29, 2024
Introduce new DSL to construct JPQL queries. Refactor ParameterMetadata to PartTreeParameterBinding.

Disable Keyset pagination with projections for Eclipselink as Eclipselink doesn't consider type hints for JPQL queries.

Closes #3588
Original pull request: #3653
mp911de pushed a commit that referenced this issue Nov 29, 2024
Make usage of ParameterExpression more explicit. Add JPQL rendering tests.
Favor Metamodel over From for building jpql queries.
Align IsNull and IsNotNull handling.
Support Derived Delete and Exists, consider null values when caching queries.

See #3588
Original pull request: #3653
mp911de added a commit that referenced this issue Nov 29, 2024
Remove method overloads accepting pure strings. Use switch-expressions. Correctly navigate nested joins. Introduce PathExpression interface, refine naming.

See #3588
Original pull request: #3653
mp911de added a commit that referenced this issue Nov 29, 2024
See #3588
Original pull request: #3653
mp911de added a commit that referenced this issue Dec 11, 2024
Introduce new DSL to construct JPQL queries. Refactor ParameterMetadata to PartTreeParameterBinding.

Disable Keyset pagination with projections for Eclipselink as Eclipselink doesn't consider type hints for JPQL queries.

Closes #3588
Original pull request: #3653
mp911de pushed a commit that referenced this issue Dec 11, 2024
Make usage of ParameterExpression more explicit. Add JPQL rendering tests.
Favor Metamodel over From for building jpql queries.
Align IsNull and IsNotNull handling.
Support Derived Delete and Exists, consider null values when caching queries.

See #3588
Original pull request: #3653
mp911de added a commit that referenced this issue Dec 11, 2024
Remove method overloads accepting pure strings. Use switch-expressions. Correctly navigate nested joins. Introduce PathExpression interface, refine naming.

See #3588
Original pull request: #3653
mp911de added a commit that referenced this issue Dec 11, 2024
See #3588
Original pull request: #3653
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants