-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Ensure proper hint is applied for projections of paginated queries for Querydsl fluent queries #2827
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,23 +15,14 @@ | |
*/ | ||
package org.springframework.data.jpa.repository; | ||
|
||
import static java.util.Arrays.asList; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType; | ||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.springframework.data.domain.Example.of; | ||
import static org.springframework.data.domain.ExampleMatcher.GenericPropertyMatcher; | ||
import static org.springframework.data.domain.ExampleMatcher.StringMatcher; | ||
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.*; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The import reformatting indicates a different threshold for There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please ignore my comment. I missed the |
||
import static org.assertj.core.api.Assertions.*; | ||
import static org.springframework.data.domain.Example.*; | ||
import static org.springframework.data.domain.ExampleMatcher.*; | ||
import static org.springframework.data.domain.Sort.Direction.*; | ||
import static org.springframework.data.jpa.domain.Specification.*; | ||
import static org.springframework.data.jpa.domain.Specification.not; | ||
import static org.springframework.data.jpa.domain.Specification.where; | ||
import static org.springframework.data.jpa.domain.sample.UserSpecifications.userHasAgeLess; | ||
import static org.springframework.data.jpa.domain.sample.UserSpecifications.userHasFirstname; | ||
import static org.springframework.data.jpa.domain.sample.UserSpecifications.userHasFirstnameLike; | ||
import static org.springframework.data.jpa.domain.sample.UserSpecifications.userHasLastname; | ||
import static org.springframework.data.jpa.domain.sample.UserSpecifications.userHasLastnameLikeWithSort; | ||
import static org.springframework.data.jpa.domain.sample.UserSpecifications.*; | ||
|
||
import jakarta.persistence.EntityManager; | ||
import jakarta.persistence.PersistenceContext; | ||
|
@@ -75,6 +66,7 @@ | |
import org.springframework.data.domain.Sort.Order; | ||
import org.springframework.data.jpa.domain.Specification; | ||
import org.springframework.data.jpa.domain.sample.Address; | ||
import org.springframework.data.jpa.domain.sample.QUser; | ||
import org.springframework.data.jpa.domain.sample.Role; | ||
import org.springframework.data.jpa.domain.sample.SpecialUser; | ||
import org.springframework.data.jpa.domain.sample.User; | ||
|
@@ -2444,6 +2436,47 @@ void findByFluentSpecificationWithSimplePropertyPathsDoesntLoadUnrequestedPaths( | |
); | ||
} | ||
|
||
@Test // GH-2820 | ||
void findByFluentPredicateWithProjectionAndPageRequest() { | ||
|
||
flushTestUsers(); | ||
em.clear(); | ||
|
||
Page<User> users = repository.findBy(QUser.user.firstname.contains("v"), q -> q // | ||
.project("firstname") // | ||
.page(PageRequest.of(0, 10))); | ||
|
||
assertThat(users).extracting(User::getFirstname).containsExactlyInAnyOrder(firstUser.getFirstname(), | ||
thirdUser.getFirstname(), fourthUser.getFirstname()); | ||
} | ||
|
||
@Test // GH-2820 | ||
void findByFluentPredicateWithProjectionAndAll() { | ||
|
||
flushTestUsers(); | ||
em.clear(); | ||
|
||
List<User> users = repository.findBy(QUser.user.firstname.contains("v"), q -> q // | ||
.project("firstname") // | ||
.all()); | ||
|
||
assertThat(users).extracting(User::getFirstname).containsExactlyInAnyOrder(firstUser.getFirstname(), | ||
thirdUser.getFirstname(), fourthUser.getFirstname()); | ||
} | ||
|
||
@Test // GH-2820 | ||
void findByFluentPredicateWithPageRequest() { | ||
|
||
flushTestUsers(); | ||
em.clear(); | ||
|
||
Page<User> users = repository.findBy(QUser.user.firstname.contains("v"), q -> q // | ||
.page(PageRequest.of(0, 10))); | ||
|
||
assertThat(users).extracting(User::getFirstname).containsExactlyInAnyOrder(firstUser.getFirstname(), | ||
thirdUser.getFirstname(), fourthUser.getFirstname()); | ||
} | ||
|
||
@Test // GH-2274 | ||
void findByFluentSpecificationWithCollectionPropertyPathsDoesntLoadUnrequestedPaths() { | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,15 +18,29 @@ | |
import jakarta.persistence.EntityManager; | ||
import jakarta.persistence.QueryHint; | ||
|
||
import java.util.*; | ||
import java.util.Collection; | ||
import java.util.Date; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.stream.Stream; | ||
|
||
import org.springframework.data.domain.*; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These changes seem fine as we have less than 10 imports. |
||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.PageRequest; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.domain.Slice; | ||
import org.springframework.data.domain.Sort; | ||
import org.springframework.data.jpa.domain.sample.Role; | ||
import org.springframework.data.jpa.domain.sample.SpecialUser; | ||
import org.springframework.data.jpa.domain.sample.User; | ||
import org.springframework.data.jpa.repository.*; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.JpaSpecificationExecutor; | ||
import org.springframework.data.jpa.repository.Modifying; | ||
import org.springframework.data.jpa.repository.Query; | ||
import org.springframework.data.jpa.repository.QueryHints; | ||
import org.springframework.data.jpa.repository.query.Procedure; | ||
import org.springframework.data.querydsl.ListQuerydslPredicateExecutor; | ||
import org.springframework.data.repository.CrudRepository; | ||
import org.springframework.data.repository.query.Param; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
@@ -45,8 +59,8 @@ | |
* @author Diego Krupitza | ||
* @author Geoffrey Deremetz | ||
*/ | ||
public interface UserRepository | ||
extends JpaRepository<User, Integer>, JpaSpecificationExecutor<User>, UserRepositoryCustom { | ||
public interface UserRepository extends JpaRepository<User, Integer>, JpaSpecificationExecutor<User>, | ||
UserRepositoryCustom, ListQuerydslPredicateExecutor<User> { | ||
|
||
/** | ||
* Retrieve users by their lastname. The finder {@literal User.findByLastname} is declared in | ||
|
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See #2721
There was a problem hiding this comment.
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
'sreadPage
method taps thecreateSortedAndProjectedQuery
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.