Skip to content
New issue

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

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

Already on GitHub? # to your account

Native query with #sort generates a broken query #3546

Closed
paulbakker opened this issue Jul 23, 2024 · 2 comments
Closed

Native query with #sort generates a broken query #3546

paulbakker opened this issue Jul 23, 2024 · 2 comments
Assignees
Labels
type: bug A general bug

Comments

@paulbakker
Copy link

Although the docs mention that dynamic sorting is not supported with native queries, the following almost works:

 @Query(
            nativeQuery = true,
            value = "select * from show #sort"
    )
    List<Show> showsNativeWithoutWorkaround(Sort sort);

It generates the following query when sorting by a title field: select * from show #sort ORDER BY title asc.
Note that it added the correct ORDER BY, but didn't remove the #sort placeholder.

I've created a simple example to reproduce here: https://github.com/paulbakker/spring-data-jpa-sort-bug.
See the README for details about the code.

The example uses a Postgres Testcontainer, so no extra setup is needed.

A hacky workaround is to add a custom query rewrite like this:

public class SortFixQueryRewriter implements QueryRewriter {
    @Override
    public String rewrite(String query, Sort sort) {
        return query.replace("#sort", "");
    }
}
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jul 23, 2024
@mp911de mp911de added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Jul 24, 2024
@mp911de
Copy link
Member

mp911de commented Jul 24, 2024

Thanks for the report. This is a leftover from the earlier restrictions on native query sorting. Meanwhile, we've added support for query enhancing through JSQLParser that should lift the restriction for many cases where the regex-based enhancer isn't capable of adding ORDER BY.

We should remove this requirement to avoid the need for custom string replacement.

@mp911de mp911de added this to the 3.2.9 (2023.1.9) milestone Jul 24, 2024
mp911de added a commit that referenced this issue Jul 24, 2024
We now no longer check for #sort in native queries to apply sorting directly. This was a leftover from earlier query rewriting.

Closes #3546
mp911de added a commit that referenced this issue Jul 24, 2024
Reformat documentation.

See #3546
mp911de added a commit that referenced this issue Jul 24, 2024
Reformat documentation.

See #3546
mp911de added a commit that referenced this issue Jul 24, 2024
We now no longer check for #sort in native queries to apply sorting directly. This was a leftover from earlier query rewriting.

Closes #3546
mp911de added a commit that referenced this issue Jul 24, 2024
Reformat documentation.

See #3546
@mp911de mp911de self-assigned this Jul 24, 2024
@paulbakker
Copy link
Author

Thanks @mp911de !

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants