diff --git a/src/main/java/org/springframework/data/jpa/repository/query/ParameterBinder.java b/src/main/java/org/springframework/data/jpa/repository/query/ParameterBinder.java index 1dc96ee1a..ddfa6b01a 100644 --- a/src/main/java/org/springframework/data/jpa/repository/query/ParameterBinder.java +++ b/src/main/java/org/springframework/data/jpa/repository/query/ParameterBinder.java @@ -19,7 +19,6 @@ import java.util.Date; import javax.persistence.Query; -import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.data.jpa.repository.query.JpaParameters.JpaParameter; @@ -27,7 +26,6 @@ import org.springframework.data.repository.query.ParameterAccessor; import org.springframework.data.repository.query.Parameters; import org.springframework.data.repository.query.ParametersParameterAccessor; import org.springframework.util.Assert; -import org.springframework.util.ObjectUtils; /** * {@link ParameterBinder} is used to bind method parameters to a {@link Query}. This is usually done whenever an @@ -131,7 +129,9 @@ public class ParameterBinder { if (parameter.isTemporalParameter()) { if (hasNamedParameter(query) && parameter.isNamedParameter()) { - query.setParameter(parameter.getName().orElseThrow(() -> new IllegalArgumentException("o_O paraneter needs to have a name!")), (Date) value, parameter.getTemporalType()); + query.setParameter( + parameter.getName().orElseThrow(() -> new IllegalArgumentException("o_O paraneter needs to have a name!")), + (Date) value, parameter.getTemporalType()); } else { query.setParameter(position, (Date) value, parameter.getTemporalType()); } @@ -139,7 +139,9 @@ public class ParameterBinder { } if (hasNamedParameter(query) && parameter.isNamedParameter()) { - query.setParameter(parameter.getName().orElseThrow(() -> new IllegalArgumentException("o_O paraneter needs to have a name!")), value); + query.setParameter( + parameter.getName().orElseThrow(() -> new IllegalArgumentException("o_O paraneter needs to have a name!")), + value); } else { query.setParameter(position, value); } @@ -163,11 +165,10 @@ public class ParameterBinder { Query result = bind(query); - if (!parameters.hasPageableParameter() || getPageable() == null || ObjectUtils.nullSafeEquals(Pageable.NONE, getPageable())) { + if (!parameters.hasPageableParameter() || getPageable().isUnpaged()) { return result; } - result.setFirstResult((int) getPageable().getOffset()); result.setMaxResults(getPageable().getPageSize()); diff --git a/src/main/java/org/springframework/data/jpa/repository/support/Querydsl.java b/src/main/java/org/springframework/data/jpa/repository/support/Querydsl.java index 07f961763..423c878c4 100644 --- a/src/main/java/org/springframework/data/jpa/repository/support/Querydsl.java +++ b/src/main/java/org/springframework/data/jpa/repository/support/Querydsl.java @@ -40,7 +40,6 @@ import com.querydsl.jpa.JPQLQuery; import com.querydsl.jpa.OpenJPATemplates; import com.querydsl.jpa.impl.AbstractJPAQuery; import com.querydsl.jpa.impl.JPAQuery; -import org.springframework.util.ObjectUtils; /** * Helper instance to ease access to Querydsl JPA query API. @@ -110,7 +109,7 @@ public class Querydsl { */ public JPQLQuery applyPagination(Pageable pageable, JPQLQuery query) { - if (pageable == null || ObjectUtils.nullSafeEquals(Pageable.NONE, pageable)) { + if (pageable.isUnpaged()) { return query; } diff --git a/src/main/java/org/springframework/data/jpa/repository/support/QuerydslJpaRepository.java b/src/main/java/org/springframework/data/jpa/repository/support/QuerydslJpaRepository.java index 170a74c81..96b4e376b 100644 --- a/src/main/java/org/springframework/data/jpa/repository/support/QuerydslJpaRepository.java +++ b/src/main/java/org/springframework/data/jpa/repository/support/QuerydslJpaRepository.java @@ -30,6 +30,7 @@ import org.springframework.data.querydsl.QSort; import org.springframework.data.querydsl.QuerydslPredicateExecutor; import org.springframework.data.querydsl.SimpleEntityPathResolver; import org.springframework.data.repository.support.PageableExecutionUtils; +import org.springframework.util.Assert; import com.querydsl.core.types.EntityPath; import com.querydsl.core.types.OrderSpecifier; @@ -77,7 +78,7 @@ public class QuerydslJpaRepository extends SimpleJpa * @param resolver must not be {@literal null}. */ public QuerydslJpaRepository(JpaEntityInformation entityInformation, EntityManager entityManager, - EntityPathResolver resolver) { + EntityPathResolver resolver) { super(entityInformation, entityManager); @@ -88,7 +89,7 @@ public class QuerydslJpaRepository extends SimpleJpa /* * (non-Javadoc) - * @see org.springframework.data.querydsl.QueryDslPredicateExecutor#findOne(com.mysema.query.types.Predicate) + * @see org.springframework.data.querydsl.QuerydslPredicateExecutor#findOne(com.mysema.query.types.Predicate) */ @Override public T findOne(Predicate predicate) { @@ -97,7 +98,7 @@ public class QuerydslJpaRepository extends SimpleJpa /* * (non-Javadoc) - * @see org.springframework.data.querydsl.QueryDslPredicateExecutor#findAll(com.mysema.query.types.Predicate) + * @see org.springframework.data.querydsl.QuerydslPredicateExecutor#findAll(com.mysema.query.types.Predicate) */ @Override public List findAll(Predicate predicate) { @@ -106,7 +107,7 @@ public class QuerydslJpaRepository extends SimpleJpa /* * (non-Javadoc) - * @see org.springframework.data.querydsl.QueryDslPredicateExecutor#findAll(com.mysema.query.types.Predicate, com.mysema.query.types.OrderSpecifier[]) + * @see org.springframework.data.querydsl.QuerydslPredicateExecutor#findAll(com.mysema.query.types.Predicate, com.mysema.query.types.OrderSpecifier[]) */ @Override public List findAll(Predicate predicate, OrderSpecifier... orders) { @@ -115,33 +116,41 @@ public class QuerydslJpaRepository extends SimpleJpa /* * (non-Javadoc) - * @see org.springframework.data.querydsl.QueryDslPredicateExecutor#findAll(com.mysema.query.types.Predicate, org.springframework.data.domain.Sort) + * @see org.springframework.data.querydsl.QuerydslPredicateExecutor#findAll(com.mysema.query.types.Predicate, org.springframework.data.domain.Sort) */ @Override public List findAll(Predicate predicate, Sort sort) { + + Assert.notNull(sort, "Sort must not be null!"); + return executeSorted(createQuery(predicate).select(path), sort); } /* * (non-Javadoc) - * @see org.springframework.data.querydsl.QueryDslPredicateExecutor#findAll(com.mysema.query.types.OrderSpecifier[]) + * @see org.springframework.data.querydsl.QuerydslPredicateExecutor#findAll(com.mysema.query.types.OrderSpecifier[]) */ @Override public List findAll(OrderSpecifier... orders) { + + Assert.notNull(orders, "Order specifiers must not be null!"); + return executeSorted(createQuery(new Predicate[0]).select(path), orders); } /* * (non-Javadoc) - * @see org.springframework.data.querydsl.QueryDslPredicateExecutor#findAll(com.mysema.query.types.Predicate, org.springframework.data.domain.Pageable) + * @see org.springframework.data.querydsl.QuerydslPredicateExecutor#findAll(com.querydsl.core.types.Predicate, org.springframework.data.domain.Pageable) */ @Override public Page findAll(Predicate predicate, Pageable pageable) { + Assert.notNull(pageable, "Pageable must not be null!"); + final JPQLQuery countQuery = createCountQuery(predicate); JPQLQuery query = querydsl.applyPagination(pageable, createQuery(predicate).select(path)); - return PageableExecutionUtils.getPage(query.fetch(), pageable == null ? Pageable.NONE : pageable, () -> countQuery.fetchCount()); + return PageableExecutionUtils.getPage(query.fetch(), pageable, () -> countQuery.fetchCount()); } /* diff --git a/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java b/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java index 14382e5e0..65dad9bd2 100644 --- a/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java +++ b/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java @@ -579,8 +579,7 @@ public class SimpleJpaRepository protected Page readPage(TypedQuery query, final Class domainClass, Pageable pageable, final Specification spec) { - if (!ObjectUtils.nullSafeEquals(Pageable.NONE, pageable)) { - + if (pageable.isPaged()) { query.setFirstResult((int) pageable.getOffset()); query.setMaxResults(pageable.getPageSize()); } diff --git a/src/test/java/org/springframework/data/jpa/repository/query/ParameterBinderUnitTests.java b/src/test/java/org/springframework/data/jpa/repository/query/ParameterBinderUnitTests.java index 482259991..cdafc47b4 100644 --- a/src/test/java/org/springframework/data/jpa/repository/query/ParameterBinderUnitTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/query/ParameterBinderUnitTests.java @@ -118,7 +118,7 @@ public class ParameterBinderUnitTests { JpaParameters parameters = new JpaParameters(method); ParameterBinder binder = new ParameterBinder(parameters, new Object[] { "foo", null }); - assertThat(binder.getPageable(), is(Pageable.NONE)); + assertThat(binder.getPageable(), is(Pageable.unpaged())); } @Test diff --git a/src/test/java/org/springframework/data/jpa/repository/query/QueryUtilsUnitTests.java b/src/test/java/org/springframework/data/jpa/repository/query/QueryUtilsUnitTests.java index 7c33168a8..1e65fe621 100644 --- a/src/test/java/org/springframework/data/jpa/repository/query/QueryUtilsUnitTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/query/QueryUtilsUnitTests.java @@ -25,6 +25,7 @@ import org.hamcrest.Matcher; import org.junit.Test; import org.springframework.dao.InvalidDataAccessApiUsageException; import org.springframework.data.domain.Sort; +import org.springframework.data.domain.Sort.Order; import org.springframework.data.jpa.domain.JpaSort; /** @@ -145,8 +146,8 @@ public class QueryUtilsUnitTests { public void doesNotPrefixOrderReferenceIfOuterJoinAliasDetected() { String query = "select p from Person p left join p.address address"; - assertThat(applySorting(query, new Sort("address.city")), endsWith("order by address.city asc")); - assertThat(applySorting(query, new Sort("address.city", "lastname"), "p"), + assertThat(applySorting(query, Sort.by("address.city")), endsWith("order by address.city asc")); + assertThat(applySorting(query, Sort.by("address.city", "lastname"), "p"), endsWith("order by address.city asc, p.lastname asc")); } @@ -154,13 +155,13 @@ public class QueryUtilsUnitTests { public void extendsExistingOrderByClausesCorrectly() { String query = "select p from Person p order by p.lastname asc"; - assertThat(applySorting(query, new Sort("firstname"), "p"), endsWith("order by p.lastname asc, p.firstname asc")); + assertThat(applySorting(query, Sort.by("firstname"), "p"), endsWith("order by p.lastname asc, p.firstname asc")); } @Test // DATAJPA-296 public void appliesIgnoreCaseOrderingCorrectly() { - Sort sort = new Sort(new Sort.Order("firstname").ignoreCase()); + Sort sort = Sort.by(Order.by("firstname").ignoreCase()); String query = "select p from Person p"; assertThat(applySorting(query, sort, "p"), endsWith("order by lower(p.firstname) asc")); @@ -169,7 +170,7 @@ public class QueryUtilsUnitTests { @Test // DATAJPA-296 public void appendsIgnoreCaseOrderingCorrectly() { - Sort sort = new Sort(new Sort.Order("firstname").ignoreCase()); + Sort sort = Sort.by(Order.by("firstname").ignoreCase()); String query = "select p from Person p order by p.lastname asc"; assertThat(applySorting(query, sort, "p"), endsWith("order by p.lastname asc, lower(p.firstname) asc")); @@ -192,7 +193,7 @@ public class QueryUtilsUnitTests { @Test(expected = InvalidDataAccessApiUsageException.class) // DATAJPA-148 public void doesNotPrefixSortsIfFunction() { - Sort sort = new Sort("sum(foo)"); + Sort sort = Sort.by("sum(foo)"); assertThat(applySorting("select p from Person p", sort, "p"), endsWith("order by sum(foo) asc")); } @@ -206,7 +207,7 @@ public class QueryUtilsUnitTests { @Test // DATAJPA-375 public void findsExistingOrderByIndependentOfCase() { - Sort sort = new Sort("lastname"); + Sort sort = Sort.by("lastname"); String query = applySorting("select p from Person p ORDER BY p.firstname", sort, "p"); assertThat(query, endsWith("ORDER BY p.firstname, p.lastname asc")); } @@ -231,7 +232,7 @@ public class QueryUtilsUnitTests { public void detectsAliassesInPlainJoins() { String query = "select p from Customer c join c.productOrder p where p.delayed = true"; - Sort sort = new Sort("p.lineItems"); + Sort sort = Sort.by("p.lineItems"); assertThat(applySorting(query, sort, "c"), endsWith("order by p.lineItems asc")); } @@ -250,7 +251,7 @@ public class QueryUtilsUnitTests { public void doesPrefixPropertyWith() { String query = "from Cat c join Dog d"; - Sort sort = new Sort("dPropertyStartingWithJoinAlias"); + Sort sort = Sort.by("dPropertyStartingWithJoinAlias"); assertThat(applySorting(query, sort, "c"), endsWith("order by c.dPropertyStartingWithJoinAlias asc")); } @@ -277,14 +278,13 @@ public class QueryUtilsUnitTests { @Test // DATAJPA-960 public void doesNotQualifySortIfNoAliasDetected() { - assertThat(applySorting("from mytable where ?1 is null", new Sort("firstname")), - endsWith("order by firstname asc")); + assertThat(applySorting("from mytable where ?1 is null", Sort.by("firstname")), endsWith("order by firstname asc")); } @Test(expected = InvalidDataAccessApiUsageException.class) // DATAJPA-965, DATAJPA-970 public void doesNotAllowWhitespaceInSort() { - Sort sort = new Sort("case when foo then bar"); + Sort sort = Sort.by("case when foo then bar"); applySorting("select p from Person p", sort, "p"); } @@ -299,7 +299,7 @@ public class QueryUtilsUnitTests { public void doesNotPrefixMultipleAliasedFunctionCalls() { String query = "SELECT AVG(m.price) AS avgPrice, SUM(m.stocks) AS sumStocks FROM Magazine m"; - Sort sort = new Sort("avgPrice", "sumStocks"); + Sort sort = Sort.by("avgPrice", "sumStocks"); assertThat(applySorting(query, sort, "m"), endsWith("order by avgPrice asc, sumStocks asc")); } @@ -308,7 +308,7 @@ public class QueryUtilsUnitTests { public void doesNotPrefixSingleAliasedFunctionCalls() { String query = "SELECT AVG(m.price) AS avgPrice FROM Magazine m"; - Sort sort = new Sort("avgPrice"); + Sort sort = Sort.by("avgPrice"); assertThat(applySorting(query, sort, "m"), endsWith("order by avgPrice asc")); } @@ -317,7 +317,7 @@ public class QueryUtilsUnitTests { public void prefixesSingleNonAliasedFunctionCallRelatedSortProperty() { String query = "SELECT AVG(m.price) AS avgPrice FROM Magazine m"; - Sort sort = new Sort("someOtherProperty"); + Sort sort = Sort.by("someOtherProperty"); assertThat(applySorting(query, sort, "m"), endsWith("order by m.someOtherProperty asc")); } @@ -326,7 +326,7 @@ public class QueryUtilsUnitTests { public void prefixesNonAliasedFunctionCallRelatedSortPropertyWhenSelectClauseContainesAliasedFunctionForDifferentProperty() { String query = "SELECT m.name, AVG(m.price) AS avgPrice FROM Magazine m"; - Sort sort = new Sort("name", "avgPrice"); + Sort sort = Sort.by("name", "avgPrice"); assertThat(applySorting(query, sort, "m"), endsWith("order by m.name asc, avgPrice asc")); } @@ -335,7 +335,7 @@ public class QueryUtilsUnitTests { public void doesNotPrefixAliasedFunctionCallNameWithMultipleNumericParameters() { String query = "SELECT SUBSTRING(m.name, 2, 5) AS trimmedName FROM Magazine m"; - Sort sort = new Sort("trimmedName"); + Sort sort = Sort.by("trimmedName"); assertThat(applySorting(query, sort, "m"), endsWith("order by trimmedName asc")); } @@ -344,7 +344,7 @@ public class QueryUtilsUnitTests { public void doesNotPrefixAliasedFunctionCallNameWithMultipleStringParameters() { String query = "SELECT CONCAT(m.name, 'foo') AS extendedName FROM Magazine m"; - Sort sort = new Sort("extendedName"); + Sort sort = Sort.by("extendedName"); assertThat(applySorting(query, sort, "m"), endsWith("order by extendedName asc")); } @@ -353,7 +353,7 @@ public class QueryUtilsUnitTests { public void doesNotPrefixAliasedFunctionCallNameWithUnderscores() { String query = "SELECT AVG(m.price) AS avg_price FROM Magazine m"; - Sort sort = new Sort("avg_price"); + Sort sort = Sort.by("avg_price"); assertThat(applySorting(query, sort, "m"), endsWith("order by avg_price asc")); } @@ -362,7 +362,7 @@ public class QueryUtilsUnitTests { public void doesNotPrefixAliasedFunctionCallNameWithDots() { String query = "SELECT AVG(m.price) AS m.avg FROM Magazine m"; - Sort sort = new Sort("m.avg"); + Sort sort = Sort.by("m.avg"); assertThat(applySorting(query, sort, "m"), endsWith("order by m.avg asc")); } @@ -371,7 +371,7 @@ public class QueryUtilsUnitTests { public void doesNotPrefixAliasedFunctionCallNameWhenQueryStringContainsMultipleWhiteSpaces() { String query = "SELECT AVG( m.price ) AS avgPrice FROM Magazine m"; - Sort sort = new Sort("avgPrice"); + Sort sort = Sort.by("avgPrice"); assertThat(applySorting(query, sort, "m"), endsWith("order by avgPrice asc")); }