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

Ensure proper hint is applied for projections of paginated queries for Querydsl fluent queries #2827

Closed
wants to merge 2 commits into from

Conversation

gregturn
Copy link
Contributor

@gregturn gregturn commented Feb 27, 2023

Resolves #2820

I have added the application of hints in case of a projection to the paginated query. I cannot seem to find a logging option to SHOW me it's working, but OP has verified it works as expected on their end. Any tips to expose this happening in our test suite would be appreciate. But apart from that, I believe test by analysis is of use in this situation.

And please alert me to any other issues with this PR.

When using Querydsl predicates to build a fluent query, be sure to apply the projection hint that generates a fetch graph.

Resolves #2820.
@gregturn gregturn requested review from schauder and mp911de February 27, 2023 18:21
@gregturn gregturn changed the title Issue/gh 2820 Ensure proper hint is applied for projections of paginated queries for Querydsl fluent queries Feb 27, 2023
Copy link
Contributor

@schauder schauder left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR looks good to me.

Regarding the test challenge, I see the following options:

  1. use a Datasource that captures the executed SQL statements.
  2. inspect the logs for the SQL statements.
  3. It seems H2 supports triggers on select statements. So one could presumably create a trigger that throws an exception, when certain columns get accessed. Of course, we currently use HSQLDB for testing and that doesn't seem to support such triggers.

Not sure if we consider it worth the effort ...

Copy link
Member

@mp911de mp911de left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left a few comments. Probably, if we had unit tests with mocking we could test things way better. Introducing SQL-capturing tests introduces a lot of maintenance afterwards because SQL can change with each Hibernate release. Probably, we're good with relying on what we do now.

import static org.springframework.data.domain.ExampleMatcher.matching;
import static org.springframework.data.domain.Sort.Direction.ASC;
import static org.springframework.data.domain.Sort.Direction.DESC;
import static java.util.Arrays.*;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The import reformatting indicates a different threshold for * imports. Please revisit your formatter settings.

Copy link
Contributor Author

@gregturn gregturn Mar 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe our standard is 1 for static imports, and I didn't have this properly configured in the past.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please ignore my comment. I missed the static.

List<R> paginatedResults = convert(pagedQuery.fetch());
AbstractJPAQuery<?, ?> query = pagedFinder.apply(sort, pageable);

if (!properties.isEmpty()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to do the same for Query by Example and Specifications?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #2721

Copy link
Contributor Author

@gregturn gregturn Mar 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be more precise, FetchableFluentQueryByExample 's readPage method taps the createSortedAndProjectedQuery method, which applies the expected hint in all scenarios.

The same can be said for the Specification-based variant.

This one is the only one that doesn't do that, probably due to the nature of Querydsl's APIs.

import java.util.stream.Stream;

import org.springframework.data.domain.*;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes seem fine as we have less than 10 imports.

@mp911de mp911de self-assigned this Mar 3, 2023
@mp911de mp911de added the type: bug A general bug label Mar 3, 2023
mp911de pushed a commit that referenced this pull request Mar 3, 2023
When using Querydsl predicates to build a fluent query, be sure to apply the projection hint that generates a fetch graph.

Resolves #2820.
Original pull request: #2827.
mp911de pushed a commit that referenced this pull request Mar 3, 2023
When using Querydsl predicates to build a fluent query, be sure to apply the projection hint that generates a fetch graph.

Resolves #2820.
Original pull request: #2827.
@mp911de
Copy link
Member

mp911de commented Mar 3, 2023

That's merged and backported now.

@mp911de mp911de closed this Mar 3, 2023
@mp911de mp911de deleted the issue/gh-2820 branch March 3, 2023 08:31
@mp911de mp911de added this to the 3.0.3 (2022.0.3) milestone Mar 3, 2023
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 this pull request may close these issues.

FetchableFluentQueryByPredicate does not respect project() call on queryFunction
3 participants