Skip to content

Commit 02937e2

Browse files
jonyschakschauder
authored andcommitted
Correcting sort object reference in FetchableFluentQueryByExample.sortBy and FetchableFluentQueryByPredicate.sortBy
Closes #2438 Original pull request #2439
1 parent 0ccf4a1 commit 02937e2

File tree

4 files changed

+91
-6
lines changed

4 files changed

+91
-6
lines changed

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/FetchableFluentQueryByExample.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 the original author or authors.
2+
* Copyright 2021-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -45,6 +45,7 @@
4545
* @author Greg Turnquist
4646
* @author Mark Paluch
4747
* @author Jens Schauder
48+
* @author J.R. Onyschak
4849
* @since 2.6
4950
*/
5051
class FetchableFluentQueryByExample<S, R> extends FluentQuerySupport<S, R> implements FetchableFluentQuery<R> {
@@ -82,8 +83,8 @@ public FetchableFluentQuery<R> sortBy(Sort sort) {
8283

8384
Assert.notNull(sort, "Sort must not be null!");
8485

85-
return new FetchableFluentQueryByExample<>(example, entityType, resultType, sort.and(sort), properties, finder,
86-
countOperation, existsOperation, entityManager, escapeCharacter);
86+
return new FetchableFluentQueryByExample<>(example, entityType, resultType, this.sort.and(sort), properties,
87+
finder, countOperation, existsOperation, entityManager, escapeCharacter);
8788
}
8889

8990
@Override

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/FetchableFluentQueryByPredicate.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 the original author or authors.
2+
* Copyright 2021-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -46,6 +46,7 @@
4646
* @author Greg Turnquist
4747
* @author Mark Paluch
4848
* @author Jens Schauder
49+
* @author J.R. Onyschak
4950
* @since 2.6
5051
*/
5152
class FetchableFluentQueryByPredicate<S, R> extends FluentQuerySupport<S, R> implements FetchableFluentQuery<R> {
@@ -85,8 +86,8 @@ public FetchableFluentQuery<R> sortBy(Sort sort) {
8586

8687
Assert.notNull(sort, "Sort must not be null!");
8788

88-
return new FetchableFluentQueryByPredicate<>(predicate, entityType, resultType, sort.and(sort), properties, finder,
89-
pagedFinder, countOperation, existsOperation, entityManager);
89+
return new FetchableFluentQueryByPredicate<>(predicate, entityType, resultType, this.sort.and(sort), properties,
90+
finder, pagedFinder, countOperation, existsOperation, entityManager);
9091
}
9192

9293
@Override
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.jpa.repository.support;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import org.junit.jupiter.api.Test;
21+
import org.springframework.data.domain.Example;
22+
import org.springframework.data.domain.Sort;
23+
import org.springframework.data.domain.Sort.Order;
24+
import org.springframework.data.jpa.repository.support.FetchableFluentQueryByExample;
25+
26+
/**
27+
* Unit tests for {@link FetchableFluentQueryByExample}.
28+
*
29+
* @author J.R. Onyschak
30+
*/
31+
class FetchableFluentQueryByExampleUnitTests {
32+
33+
@Test // GH-2438
34+
@SuppressWarnings({ "rawtypes", "unchecked" })
35+
void multipleSortBy() {
36+
Sort s1 = Sort.by(Order.by("s1"));
37+
Sort s2 = Sort.by(Order.by("s2"));
38+
FetchableFluentQueryByExample f = new FetchableFluentQueryByExample(Example.of(""), null, null, null, null, null);
39+
f = (FetchableFluentQueryByExample) f.sortBy(s1).sortBy(s2);
40+
assertThat(f.sort).isEqualTo(s1.and(s2));
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.jpa.repository.support;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import org.junit.jupiter.api.Test;
21+
import org.springframework.data.domain.Sort;
22+
import org.springframework.data.domain.Sort.Order;
23+
import org.springframework.data.jpa.repository.support.FetchableFluentQueryByPredicate;
24+
25+
/**
26+
* Unit tests for {@link FetchableFluentQueryByPredicate}.
27+
*
28+
* @author J.R. Onyschak
29+
*/
30+
class FetchableFluentQueryByPredicateUnitTests {
31+
32+
@Test // GH-2438
33+
@SuppressWarnings({ "rawtypes", "unchecked" })
34+
void multipleSortBy() {
35+
Sort s1 = Sort.by(Order.by("s1"));
36+
Sort s2 = Sort.by(Order.by("s2"));
37+
FetchableFluentQueryByPredicate f = new FetchableFluentQueryByPredicate(null, null, null, null, null, null, null);
38+
f = (FetchableFluentQueryByPredicate) f.sortBy(s1).sortBy(s2);
39+
assertThat(f.sort).isEqualTo(s1.and(s2));
40+
}
41+
}

0 commit comments

Comments
 (0)