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

Explore using JPQL queries instead of Criteria Queries #3588

Open
1 of 5 tasks
mp911de opened this issue Aug 23, 2024 · 5 comments
Open
1 of 5 tasks

Explore using JPQL queries instead of Criteria 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.

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

When branches are created from issues, their pull requests are automatically linked.

3 participants