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

Generating countQuery for queries with CTE fails with actual pagination #3726

Closed
bountin opened this issue Dec 27, 2024 · 2 comments
Closed
Labels
type: bug A general bug

Comments

@bountin
Copy link

bountin commented Dec 27, 2024

I rechecked the "Automatic countQuery with CTE" case from #3504 and found another adjacent issue. In the other bug report I've used Pageable.unpaged() which apparently doesn't actually execute a count because the query itself already returns all elements. I've updated the repository with a constrained page request and now I have another exception from Hibernate not accepting the generated query:

org.hibernate.query.SemanticException: The derived SqmFrom[id, number] can not be used in a context where the expression needs to be expanded to identifying parts, because a derived model part does not have identifying parts. Replace uses of the root with paths instead e.g. `derivedRoot.get("alias1")` or `derivedRoot.alias1`

The generated countQuery is WITH entities AS (SELECT e.id as id, e.number as number FROM TestEntity e) SELECT count(c) FROM entities c.

See again here: https://github.com/bountin/spring-data-jpa-issue-3504

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Dec 27, 2024
@bountin
Copy link
Author

bountin commented Dec 27, 2024

Ah, my current workaround is to use an explicit countQuery (see below) but I'd expect Spring Data to either block the invalid JQL generation with an explaining exception or actually support this usecase :)

    private static final String CTE_QUERY = "WITH foo AS (......)"
    private static final String SELECT = "SELECT field1, ... FROM foo foo"
    private static final String COUNT = "SELECT count(*) FROM foo foo"

    @Query(value = CTE_QUERY + SELECT, countQuery = CTE_QUERY + COUNT)

@nexus061
Copy link

nexus061 commented Jan 7, 2025

Ah, my current workaround is to use an explicit countQuery (see below) but I'd expect Spring Data to either block the invalid JQL generation with an explaining exception or actually support this usecase :)

    private static final String CTE_QUERY = "WITH foo AS (......)"
    private static final String SELECT = "SELECT field1, ... FROM foo foo"
    private static final String COUNT = "SELECT count(*) FROM foo foo"

    @Query(value = CTE_QUERY + SELECT, countQuery = CTE_QUERY + COUNT)

I get it too, I managed to make it work also using countProjection.

@mp911de mp911de linked a pull request Jan 13, 2025 that will close this issue
@mp911de mp911de added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Jan 13, 2025
@mp911de mp911de added this to the 3.4.2 (2024.1.2) milestone Jan 13, 2025
mp911de added a commit that referenced this issue Jan 13, 2025
Introduce QueryInformation to provide query introspection results. Refine generics.

See #3726
Original pull request: #3730
mp911de pushed a commit that referenced this issue Jan 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants