Skip to content

Pageable results and @Query annotation #1843

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

Closed
edovac opened this issue Jun 14, 2021 · 4 comments · Fixed by #1844
Closed

Pageable results and @Query annotation #1843

edovac opened this issue Jun 14, 2021 · 4 comments · Fixed by #1844
Assignees
Labels
type: bug A general bug

Comments

@edovac
Copy link

edovac commented Jun 14, 2021

Spring Boot 2.4.6 and 2.4.7 broke compatibility between Page result type and @query annotation on same method.

A method with this signature used to work up to Spring Boot 2.4.5:

@Query("{\"match_phrase_prefix\":{\"stringValue\":\"?0\"}}")
Page<MyDomainObj> findByStringPrefix(String stringValue, Pageable pageable);

Starting from Spring Boot 2.4.6 whenever the code access to the objects inside the List a class cast exception is thrown.

See the attached test project.
The problem is shown with current setup. To replace it's behaviour with the previous version just replace Spring Boot version in pom.xml

demo.zip

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jun 14, 2021
@sothawo sothawo added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Jun 14, 2021
@sothawo
Copy link
Collaborator

sothawo commented Jun 14, 2021

This regression seems to be caused by the backport of #1811 into the 4.1 branch where the processing of @Query annotataed methods returns now a SearchPage<T> which basically is a Page<SearchHit<T>>.

That this in version 4.2 works without an error comes from a previous change in 4.2 that cleaned up old code. I will add a fix to the 4.1 branch.

What you could do with the current version: Change the return type of your repository method to:

@Query("{\"match_phrase_prefix\":{\"stringValue\":\"?0\"}}")
SearchPage<MyDomainObj> findByStringPrefix(String stringValue, Pageable pageable);

and then in your code work with the SearchHit<T> objects; you could get a list of the entites with:

SearchPage<MyDomainObj> result = repository.findByStringPrefix("a ", Pageable.unpaged());
List<MyDomainObj> list = result.getContent().stream().map(SearchHit::getContent).collect(Collectors.toList());

@sothawo sothawo added this to the 4.1.10 (2020.0.10) milestone Jun 14, 2021
@edovac
Copy link
Author

edovac commented Jun 15, 2021

As I understand the SearchPage<T> will be the new standard for version 4.2.
The old convention seemed more appropriate because it allowed returning Page<T> directly from REST methods thus integrating REST layer and Spring Data layer. Maybe I'm missing something in the latest releases.

We have only a few points where whe use the @Query annotation with Pageable and I think we can refactor the code.

Thanks

@sothawo sothawo self-assigned this Jun 15, 2021
sothawo added a commit that referenced this issue Jun 15, 2021
Original Pull Request #1844
Closes #1843
@sothawo sothawo linked a pull request Jun 15, 2021 that will close this issue
@sothawo
Copy link
Collaborator

sothawo commented Jun 15, 2021

If you define your method to return a SearchPage<T>you get a SearchPage<T> which is a Page<SearchHit<T>>. If the return type is Page<T> you get a Page. That this produces an error in your case was a regression as I wrote.
This is fixed in the 4.1.x branch and will be contained in the next SR

@sothawo sothawo closed this as completed Jun 15, 2021
@edovac
Copy link
Author

edovac commented Jun 16, 2021

Ok, this is clear. Thanks

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.

3 participants