Skip to content

Commit 0fb18b1

Browse files
committed
DATAJPA-1064 - Adapted to changes in Pageable and Sort API.
Removed null-checks for Pageable. Use factory methods for Sort.
1 parent 59d6587 commit 0fb18b1

File tree

6 files changed

+48
-40
lines changed

6 files changed

+48
-40
lines changed

src/main/java/org/springframework/data/jpa/repository/query/ParameterBinder.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,13 @@
1919

2020
import javax.persistence.Query;
2121

22-
import org.springframework.data.domain.Page;
2322
import org.springframework.data.domain.Pageable;
2423
import org.springframework.data.domain.Sort;
2524
import org.springframework.data.jpa.repository.query.JpaParameters.JpaParameter;
2625
import org.springframework.data.repository.query.ParameterAccessor;
2726
import org.springframework.data.repository.query.Parameters;
2827
import org.springframework.data.repository.query.ParametersParameterAccessor;
2928
import org.springframework.util.Assert;
30-
import org.springframework.util.ObjectUtils;
3129

3230
/**
3331
* {@link ParameterBinder} is used to bind method parameters to a {@link Query}. This is usually done whenever an
@@ -131,15 +129,19 @@ protected void bind(Query query, JpaParameter parameter, Object value, int posit
131129

132130
if (parameter.isTemporalParameter()) {
133131
if (hasNamedParameter(query) && parameter.isNamedParameter()) {
134-
query.setParameter(parameter.getName().orElseThrow(() -> new IllegalArgumentException("o_O paraneter needs to have a name!")), (Date) value, parameter.getTemporalType());
132+
query.setParameter(
133+
parameter.getName().orElseThrow(() -> new IllegalArgumentException("o_O paraneter needs to have a name!")),
134+
(Date) value, parameter.getTemporalType());
135135
} else {
136136
query.setParameter(position, (Date) value, parameter.getTemporalType());
137137
}
138138
return;
139139
}
140140

141141
if (hasNamedParameter(query) && parameter.isNamedParameter()) {
142-
query.setParameter(parameter.getName().orElseThrow(() -> new IllegalArgumentException("o_O paraneter needs to have a name!")), value);
142+
query.setParameter(
143+
parameter.getName().orElseThrow(() -> new IllegalArgumentException("o_O paraneter needs to have a name!")),
144+
value);
143145
} else {
144146
query.setParameter(position, value);
145147
}
@@ -163,11 +165,10 @@ private Query bindAndPrepare(Query query, Parameters<?, ?> parameters) {
163165

164166
Query result = bind(query);
165167

166-
if (!parameters.hasPageableParameter() || getPageable() == null || ObjectUtils.nullSafeEquals(Pageable.NONE, getPageable())) {
168+
if (!parameters.hasPageableParameter() || getPageable().isUnpaged()) {
167169
return result;
168170
}
169171

170-
171172
result.setFirstResult((int) getPageable().getOffset());
172173
result.setMaxResults(getPageable().getPageSize());
173174

src/main/java/org/springframework/data/jpa/repository/support/Querydsl.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import com.querydsl.jpa.OpenJPATemplates;
4141
import com.querydsl.jpa.impl.AbstractJPAQuery;
4242
import com.querydsl.jpa.impl.JPAQuery;
43-
import org.springframework.util.ObjectUtils;
4443

4544
/**
4645
* Helper instance to ease access to Querydsl JPA query API.
@@ -110,7 +109,7 @@ public AbstractJPAQuery<Object, JPAQuery<Object>> createQuery(EntityPath<?>... p
110109
*/
111110
public <T> JPQLQuery<T> applyPagination(Pageable pageable, JPQLQuery<T> query) {
112111

113-
if (pageable == null || ObjectUtils.nullSafeEquals(Pageable.NONE, pageable)) {
112+
if (pageable.isUnpaged()) {
114113
return query;
115114
}
116115

src/main/java/org/springframework/data/jpa/repository/support/QuerydslJpaRepository.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
3131
import org.springframework.data.querydsl.SimpleEntityPathResolver;
3232
import org.springframework.data.repository.support.PageableExecutionUtils;
33+
import org.springframework.util.Assert;
3334

3435
import com.querydsl.core.types.EntityPath;
3536
import com.querydsl.core.types.OrderSpecifier;
@@ -77,7 +78,7 @@ public QuerydslJpaRepository(JpaEntityInformation<T, ID> entityInformation, Enti
7778
* @param resolver must not be {@literal null}.
7879
*/
7980
public QuerydslJpaRepository(JpaEntityInformation<T, ID> entityInformation, EntityManager entityManager,
80-
EntityPathResolver resolver) {
81+
EntityPathResolver resolver) {
8182

8283
super(entityInformation, entityManager);
8384

@@ -88,7 +89,7 @@ public QuerydslJpaRepository(JpaEntityInformation<T, ID> entityInformation, Enti
8889

8990
/*
9091
* (non-Javadoc)
91-
* @see org.springframework.data.querydsl.QueryDslPredicateExecutor#findOne(com.mysema.query.types.Predicate)
92+
* @see org.springframework.data.querydsl.QuerydslPredicateExecutor#findOne(com.mysema.query.types.Predicate)
9293
*/
9394
@Override
9495
public T findOne(Predicate predicate) {
@@ -97,7 +98,7 @@ public T findOne(Predicate predicate) {
9798

9899
/*
99100
* (non-Javadoc)
100-
* @see org.springframework.data.querydsl.QueryDslPredicateExecutor#findAll(com.mysema.query.types.Predicate)
101+
* @see org.springframework.data.querydsl.QuerydslPredicateExecutor#findAll(com.mysema.query.types.Predicate)
101102
*/
102103
@Override
103104
public List<T> findAll(Predicate predicate) {
@@ -106,7 +107,7 @@ public List<T> findAll(Predicate predicate) {
106107

107108
/*
108109
* (non-Javadoc)
109-
* @see org.springframework.data.querydsl.QueryDslPredicateExecutor#findAll(com.mysema.query.types.Predicate, com.mysema.query.types.OrderSpecifier<?>[])
110+
* @see org.springframework.data.querydsl.QuerydslPredicateExecutor#findAll(com.mysema.query.types.Predicate, com.mysema.query.types.OrderSpecifier<?>[])
110111
*/
111112
@Override
112113
public List<T> findAll(Predicate predicate, OrderSpecifier<?>... orders) {
@@ -115,33 +116,41 @@ public List<T> findAll(Predicate predicate, OrderSpecifier<?>... orders) {
115116

116117
/*
117118
* (non-Javadoc)
118-
* @see org.springframework.data.querydsl.QueryDslPredicateExecutor#findAll(com.mysema.query.types.Predicate, org.springframework.data.domain.Sort)
119+
* @see org.springframework.data.querydsl.QuerydslPredicateExecutor#findAll(com.mysema.query.types.Predicate, org.springframework.data.domain.Sort)
119120
*/
120121
@Override
121122
public List<T> findAll(Predicate predicate, Sort sort) {
123+
124+
Assert.notNull(sort, "Sort must not be null!");
125+
122126
return executeSorted(createQuery(predicate).select(path), sort);
123127
}
124128

125129
/*
126130
* (non-Javadoc)
127-
* @see org.springframework.data.querydsl.QueryDslPredicateExecutor#findAll(com.mysema.query.types.OrderSpecifier[])
131+
* @see org.springframework.data.querydsl.QuerydslPredicateExecutor#findAll(com.mysema.query.types.OrderSpecifier[])
128132
*/
129133
@Override
130134
public List<T> findAll(OrderSpecifier<?>... orders) {
135+
136+
Assert.notNull(orders, "Order specifiers must not be null!");
137+
131138
return executeSorted(createQuery(new Predicate[0]).select(path), orders);
132139
}
133140

134141
/*
135142
* (non-Javadoc)
136-
* @see org.springframework.data.querydsl.QueryDslPredicateExecutor#findAll(com.mysema.query.types.Predicate, org.springframework.data.domain.Pageable)
143+
* @see org.springframework.data.querydsl.QuerydslPredicateExecutor#findAll(com.querydsl.core.types.Predicate, org.springframework.data.domain.Pageable)
137144
*/
138145
@Override
139146
public Page<T> findAll(Predicate predicate, Pageable pageable) {
140147

148+
Assert.notNull(pageable, "Pageable must not be null!");
149+
141150
final JPQLQuery<?> countQuery = createCountQuery(predicate);
142151
JPQLQuery<T> query = querydsl.applyPagination(pageable, createQuery(predicate).select(path));
143152

144-
return PageableExecutionUtils.getPage(query.fetch(), pageable == null ? Pageable.NONE : pageable, () -> countQuery.fetchCount());
153+
return PageableExecutionUtils.getPage(query.fetch(), pageable, () -> countQuery.fetchCount());
145154
}
146155

147156
/*

src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -579,8 +579,7 @@ protected Page<T> readPage(TypedQuery<T> query, Pageable pageable, Specification
579579
protected <S extends T> Page<S> readPage(TypedQuery<S> query, final Class<S> domainClass, Pageable pageable,
580580
final Specification<S> spec) {
581581

582-
if (!ObjectUtils.nullSafeEquals(Pageable.NONE, pageable)) {
583-
582+
if (pageable.isPaged()) {
584583
query.setFirstResult((int) pageable.getOffset());
585584
query.setMaxResults(pageable.getPageSize());
586585
}

src/test/java/org/springframework/data/jpa/repository/query/ParameterBinderUnitTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public void returnsPageableNoneIfNoPageableWasProvided() throws SecurityExceptio
118118
JpaParameters parameters = new JpaParameters(method);
119119
ParameterBinder binder = new ParameterBinder(parameters, new Object[] { "foo", null });
120120

121-
assertThat(binder.getPageable(), is(Pageable.NONE));
121+
assertThat(binder.getPageable(), is(Pageable.unpaged()));
122122
}
123123

124124
@Test

src/test/java/org/springframework/data/jpa/repository/query/QueryUtilsUnitTests.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.junit.Test;
2626
import org.springframework.dao.InvalidDataAccessApiUsageException;
2727
import org.springframework.data.domain.Sort;
28+
import org.springframework.data.domain.Sort.Order;
2829
import org.springframework.data.jpa.domain.JpaSort;
2930

3031
/**
@@ -145,22 +146,22 @@ public void detectsJoinAliasesCorrectly() {
145146
public void doesNotPrefixOrderReferenceIfOuterJoinAliasDetected() {
146147

147148
String query = "select p from Person p left join p.address address";
148-
assertThat(applySorting(query, new Sort("address.city")), endsWith("order by address.city asc"));
149-
assertThat(applySorting(query, new Sort("address.city", "lastname"), "p"),
149+
assertThat(applySorting(query, Sort.by("address.city")), endsWith("order by address.city asc"));
150+
assertThat(applySorting(query, Sort.by("address.city", "lastname"), "p"),
150151
endsWith("order by address.city asc, p.lastname asc"));
151152
}
152153

153154
@Test // DATAJPA-252
154155
public void extendsExistingOrderByClausesCorrectly() {
155156

156157
String query = "select p from Person p order by p.lastname asc";
157-
assertThat(applySorting(query, new Sort("firstname"), "p"), endsWith("order by p.lastname asc, p.firstname asc"));
158+
assertThat(applySorting(query, Sort.by("firstname"), "p"), endsWith("order by p.lastname asc, p.firstname asc"));
158159
}
159160

160161
@Test // DATAJPA-296
161162
public void appliesIgnoreCaseOrderingCorrectly() {
162163

163-
Sort sort = new Sort(new Sort.Order("firstname").ignoreCase());
164+
Sort sort = Sort.by(Order.by("firstname").ignoreCase());
164165

165166
String query = "select p from Person p";
166167
assertThat(applySorting(query, sort, "p"), endsWith("order by lower(p.firstname) asc"));
@@ -169,7 +170,7 @@ public void appliesIgnoreCaseOrderingCorrectly() {
169170
@Test // DATAJPA-296
170171
public void appendsIgnoreCaseOrderingCorrectly() {
171172

172-
Sort sort = new Sort(new Sort.Order("firstname").ignoreCase());
173+
Sort sort = Sort.by(Order.by("firstname").ignoreCase());
173174

174175
String query = "select p from Person p order by p.lastname asc";
175176
assertThat(applySorting(query, sort, "p"), endsWith("order by p.lastname asc, lower(p.firstname) asc"));
@@ -192,7 +193,7 @@ public void projectsCOuntQueriesForQueriesWithSubselects() {
192193
@Test(expected = InvalidDataAccessApiUsageException.class) // DATAJPA-148
193194
public void doesNotPrefixSortsIfFunction() {
194195

195-
Sort sort = new Sort("sum(foo)");
196+
Sort sort = Sort.by("sum(foo)");
196197
assertThat(applySorting("select p from Person p", sort, "p"), endsWith("order by sum(foo) asc"));
197198
}
198199

@@ -206,7 +207,7 @@ public void removesOrderByInGeneratedCountQueryFromOriginalQueryIfPresent() {
206207
@Test // DATAJPA-375
207208
public void findsExistingOrderByIndependentOfCase() {
208209

209-
Sort sort = new Sort("lastname");
210+
Sort sort = Sort.by("lastname");
210211
String query = applySorting("select p from Person p ORDER BY p.firstname", sort, "p");
211212
assertThat(query, endsWith("ORDER BY p.firstname, p.lastname asc"));
212213
}
@@ -231,7 +232,7 @@ public void createCountQueryFromTheGivenCountProjection() {
231232
public void detectsAliassesInPlainJoins() {
232233

233234
String query = "select p from Customer c join c.productOrder p where p.delayed = true";
234-
Sort sort = new Sort("p.lineItems");
235+
Sort sort = Sort.by("p.lineItems");
235236

236237
assertThat(applySorting(query, sort, "c"), endsWith("order by p.lineItems asc"));
237238
}
@@ -250,7 +251,7 @@ public void detectsAliasInQueryContainingLineBreaks() {
250251
public void doesPrefixPropertyWith() {
251252

252253
String query = "from Cat c join Dog d";
253-
Sort sort = new Sort("dPropertyStartingWithJoinAlias");
254+
Sort sort = Sort.by("dPropertyStartingWithJoinAlias");
254255

255256
assertThat(applySorting(query, sort, "c"), endsWith("order by c.dPropertyStartingWithJoinAlias asc"));
256257
}
@@ -277,14 +278,13 @@ public void detectsConstructorExpressionWithLineBreaks() {
277278

278279
@Test // DATAJPA-960
279280
public void doesNotQualifySortIfNoAliasDetected() {
280-
assertThat(applySorting("from mytable where ?1 is null", new Sort("firstname")),
281-
endsWith("order by firstname asc"));
281+
assertThat(applySorting("from mytable where ?1 is null", Sort.by("firstname")), endsWith("order by firstname asc"));
282282
}
283283

284284
@Test(expected = InvalidDataAccessApiUsageException.class) // DATAJPA-965, DATAJPA-970
285285
public void doesNotAllowWhitespaceInSort() {
286286

287-
Sort sort = new Sort("case when foo then bar");
287+
Sort sort = Sort.by("case when foo then bar");
288288
applySorting("select p from Person p", sort, "p");
289289
}
290290

@@ -299,7 +299,7 @@ public void doesNotPrefixUnsageJpaSortFunctionCalls() {
299299
public void doesNotPrefixMultipleAliasedFunctionCalls() {
300300

301301
String query = "SELECT AVG(m.price) AS avgPrice, SUM(m.stocks) AS sumStocks FROM Magazine m";
302-
Sort sort = new Sort("avgPrice", "sumStocks");
302+
Sort sort = Sort.by("avgPrice", "sumStocks");
303303

304304
assertThat(applySorting(query, sort, "m"), endsWith("order by avgPrice asc, sumStocks asc"));
305305
}
@@ -308,7 +308,7 @@ public void doesNotPrefixMultipleAliasedFunctionCalls() {
308308
public void doesNotPrefixSingleAliasedFunctionCalls() {
309309

310310
String query = "SELECT AVG(m.price) AS avgPrice FROM Magazine m";
311-
Sort sort = new Sort("avgPrice");
311+
Sort sort = Sort.by("avgPrice");
312312

313313
assertThat(applySorting(query, sort, "m"), endsWith("order by avgPrice asc"));
314314
}
@@ -317,7 +317,7 @@ public void doesNotPrefixSingleAliasedFunctionCalls() {
317317
public void prefixesSingleNonAliasedFunctionCallRelatedSortProperty() {
318318

319319
String query = "SELECT AVG(m.price) AS avgPrice FROM Magazine m";
320-
Sort sort = new Sort("someOtherProperty");
320+
Sort sort = Sort.by("someOtherProperty");
321321

322322
assertThat(applySorting(query, sort, "m"), endsWith("order by m.someOtherProperty asc"));
323323
}
@@ -326,7 +326,7 @@ public void prefixesSingleNonAliasedFunctionCallRelatedSortProperty() {
326326
public void prefixesNonAliasedFunctionCallRelatedSortPropertyWhenSelectClauseContainesAliasedFunctionForDifferentProperty() {
327327

328328
String query = "SELECT m.name, AVG(m.price) AS avgPrice FROM Magazine m";
329-
Sort sort = new Sort("name", "avgPrice");
329+
Sort sort = Sort.by("name", "avgPrice");
330330

331331
assertThat(applySorting(query, sort, "m"), endsWith("order by m.name asc, avgPrice asc"));
332332
}
@@ -335,7 +335,7 @@ public void prefixesNonAliasedFunctionCallRelatedSortPropertyWhenSelectClauseCon
335335
public void doesNotPrefixAliasedFunctionCallNameWithMultipleNumericParameters() {
336336

337337
String query = "SELECT SUBSTRING(m.name, 2, 5) AS trimmedName FROM Magazine m";
338-
Sort sort = new Sort("trimmedName");
338+
Sort sort = Sort.by("trimmedName");
339339

340340
assertThat(applySorting(query, sort, "m"), endsWith("order by trimmedName asc"));
341341
}
@@ -344,7 +344,7 @@ public void doesNotPrefixAliasedFunctionCallNameWithMultipleNumericParameters()
344344
public void doesNotPrefixAliasedFunctionCallNameWithMultipleStringParameters() {
345345

346346
String query = "SELECT CONCAT(m.name, 'foo') AS extendedName FROM Magazine m";
347-
Sort sort = new Sort("extendedName");
347+
Sort sort = Sort.by("extendedName");
348348

349349
assertThat(applySorting(query, sort, "m"), endsWith("order by extendedName asc"));
350350
}
@@ -353,7 +353,7 @@ public void doesNotPrefixAliasedFunctionCallNameWithMultipleStringParameters() {
353353
public void doesNotPrefixAliasedFunctionCallNameWithUnderscores() {
354354

355355
String query = "SELECT AVG(m.price) AS avg_price FROM Magazine m";
356-
Sort sort = new Sort("avg_price");
356+
Sort sort = Sort.by("avg_price");
357357

358358
assertThat(applySorting(query, sort, "m"), endsWith("order by avg_price asc"));
359359
}
@@ -362,7 +362,7 @@ public void doesNotPrefixAliasedFunctionCallNameWithUnderscores() {
362362
public void doesNotPrefixAliasedFunctionCallNameWithDots() {
363363

364364
String query = "SELECT AVG(m.price) AS m.avg FROM Magazine m";
365-
Sort sort = new Sort("m.avg");
365+
Sort sort = Sort.by("m.avg");
366366

367367
assertThat(applySorting(query, sort, "m"), endsWith("order by m.avg asc"));
368368
}
@@ -371,7 +371,7 @@ public void doesNotPrefixAliasedFunctionCallNameWithDots() {
371371
public void doesNotPrefixAliasedFunctionCallNameWhenQueryStringContainsMultipleWhiteSpaces() {
372372

373373
String query = "SELECT AVG( m.price ) AS avgPrice FROM Magazine m";
374-
Sort sort = new Sort("avgPrice");
374+
Sort sort = Sort.by("avgPrice");
375375

376376
assertThat(applySorting(query, sort, "m"), endsWith("order by avgPrice asc"));
377377
}

0 commit comments

Comments
 (0)