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

Documentation Improvement: countQuery #2831

Closed
Tracked by #2846
nightswimmings opened this issue Feb 28, 2023 · 2 comments
Closed
Tracked by #2846

Documentation Improvement: countQuery #2831

nightswimmings opened this issue Feb 28, 2023 · 2 comments
Labels
status: superseded An issue that has been superseded by another type: documentation A documentation update

Comments

@nightswimmings
Copy link

nightswimmings commented Feb 28, 2023

By reading https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.at-query.native, I realised there are few important aspects that I think it would be interesting to extend:

1.On native queries part, it is not clear what dynamic sorting means in this context (passing a Sort, Pageable)?
2.The following paragraph it says that Pageable is supported, I think it is not clear whether it applies to the Sort part in it?
3.Then it says the countQuery is needed, but Ive seen examples with native queries and countQuery not being needed, so I guess there must be some counterpart.
4. Besides, non-native @queries are not mentioned, does that mean that countQuery is completely useless on those? How does JPA perform the magic counting? By issuing the whole query and counting the results? Or by rewriting it as SELECT count(*) internally? If its the former, I believe there should be a note stating that even JPA can infer the totals, it's always advised to provide the query yourself for performance

Thanks guys! Your project is amazing

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Feb 28, 2023
@mp911de
Copy link
Member

mp911de commented Mar 10, 2023

Spring Data JPA does not currently support dynamic sorting for native queries, because it would have to manipulate the actual query declared, which it cannot do reliably for native SQL.

is intentionally vague as rewriting queries for sorting works only for simple cases. More complex queries (with fancy aliases, subqueries, join, unions) can work but that's not guaranteed. Since Pageable contains a Sort as well, it's obvious that when talking about sorting and you see Pageable, the context applies.

Similar goes for count queries. We can derive these in a lot of cases, but not all.

How does JPA perform the magic counting?

JPA implementations have a JPQL parser and they extract what's necessary. We don't have it (yet).

I'd like to keep this ticket open until we implement #2846. Once that is in place, we can refine our query parsing capabilities.

@mp911de mp911de added type: documentation A documentation update status: blocked An issue that's blocked on an external project change and removed status: waiting-for-triage An issue we've not yet triaged labels Mar 10, 2023
@mp911de
Copy link
Member

mp911de commented Jan 31, 2025

That's already in place. The docs mention:

Spring Data can rewrite simple queries for pagination and sorting. More complex queries require either JSqlParser to be on the class path or a countQuery declared in your code. See the example below for more details.

@mp911de mp911de closed this as not planned Won't fix, can't repro, duplicate, stale Jan 31, 2025
@mp911de mp911de added status: superseded An issue that has been superseded by another and removed status: blocked An issue that's blocked on an external project change labels Jan 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: superseded An issue that has been superseded by another type: documentation A documentation update
Projects
None yet
Development

No branches or pull requests

3 participants