diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/FetchableFluentQueryByExample.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/FetchableFluentQueryByExample.java index 96a99684b..f2f0d7c55 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/FetchableFluentQueryByExample.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/FetchableFluentQueryByExample.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 the original author or authors. + * Copyright 2021-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -45,6 +45,7 @@ import org.springframework.util.Assert; * @author Greg Turnquist * @author Mark Paluch * @author Jens Schauder + * @author J.R. Onyschak * @since 2.6 */ class FetchableFluentQueryByExample extends FluentQuerySupport implements FetchableFluentQuery { @@ -82,8 +83,8 @@ class FetchableFluentQueryByExample extends FluentQuerySupport imple Assert.notNull(sort, "Sort must not be null!"); - return new FetchableFluentQueryByExample<>(example, entityType, resultType, sort.and(sort), properties, finder, - countOperation, existsOperation, entityManager, escapeCharacter); + return new FetchableFluentQueryByExample<>(example, entityType, resultType, this.sort.and(sort), properties, + finder, countOperation, existsOperation, entityManager, escapeCharacter); } @Override diff --git a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/FetchableFluentQueryByPredicate.java b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/FetchableFluentQueryByPredicate.java index 009d89159..20fe4b717 100644 --- a/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/FetchableFluentQueryByPredicate.java +++ b/spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/support/FetchableFluentQueryByPredicate.java @@ -1,5 +1,5 @@ /* - * Copyright 2021 the original author or authors. + * Copyright 2021-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -46,6 +46,7 @@ import com.querydsl.jpa.impl.AbstractJPAQuery; * @author Greg Turnquist * @author Mark Paluch * @author Jens Schauder + * @author J.R. Onyschak * @since 2.6 */ class FetchableFluentQueryByPredicate extends FluentQuerySupport implements FetchableFluentQuery { @@ -85,8 +86,8 @@ class FetchableFluentQueryByPredicate extends FluentQuerySupport imp Assert.notNull(sort, "Sort must not be null!"); - return new FetchableFluentQueryByPredicate<>(predicate, entityType, resultType, sort.and(sort), properties, finder, - pagedFinder, countOperation, existsOperation, entityManager); + return new FetchableFluentQueryByPredicate<>(predicate, entityType, resultType, this.sort.and(sort), properties, + finder, pagedFinder, countOperation, existsOperation, entityManager); } @Override diff --git a/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/support/FetchableFluentQueryByExampleUnitTests.java b/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/support/FetchableFluentQueryByExampleUnitTests.java new file mode 100644 index 000000000..0faf4593a --- /dev/null +++ b/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/support/FetchableFluentQueryByExampleUnitTests.java @@ -0,0 +1,42 @@ +/* + * Copyright 2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.jpa.repository.support; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.jupiter.api.Test; +import org.springframework.data.domain.Example; +import org.springframework.data.domain.Sort; +import org.springframework.data.domain.Sort.Order; +import org.springframework.data.jpa.repository.support.FetchableFluentQueryByExample; + +/** + * Unit tests for {@link FetchableFluentQueryByExample}. + * + * @author J.R. Onyschak + */ +class FetchableFluentQueryByExampleUnitTests { + + @Test // GH-2438 + @SuppressWarnings({ "rawtypes", "unchecked" }) + void multipleSortBy() { + Sort s1 = Sort.by(Order.by("s1")); + Sort s2 = Sort.by(Order.by("s2")); + FetchableFluentQueryByExample f = new FetchableFluentQueryByExample(Example.of(""), null, null, null, null, null); + f = (FetchableFluentQueryByExample) f.sortBy(s1).sortBy(s2); + assertThat(f.sort).isEqualTo(s1.and(s2)); + } +} diff --git a/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/support/FetchableFluentQueryByPredicateUnitTests.java b/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/support/FetchableFluentQueryByPredicateUnitTests.java new file mode 100644 index 000000000..7f59794c1 --- /dev/null +++ b/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/support/FetchableFluentQueryByPredicateUnitTests.java @@ -0,0 +1,41 @@ +/* + * Copyright 2022 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.data.jpa.repository.support; + +import static org.assertj.core.api.Assertions.*; + +import org.junit.jupiter.api.Test; +import org.springframework.data.domain.Sort; +import org.springframework.data.domain.Sort.Order; +import org.springframework.data.jpa.repository.support.FetchableFluentQueryByPredicate; + +/** + * Unit tests for {@link FetchableFluentQueryByPredicate}. + * + * @author J.R. Onyschak + */ +class FetchableFluentQueryByPredicateUnitTests { + + @Test // GH-2438 + @SuppressWarnings({ "rawtypes", "unchecked" }) + void multipleSortBy() { + Sort s1 = Sort.by(Order.by("s1")); + Sort s2 = Sort.by(Order.by("s2")); + FetchableFluentQueryByPredicate f = new FetchableFluentQueryByPredicate(null, null, null, null, null, null, null); + f = (FetchableFluentQueryByPredicate) f.sortBy(s1).sortBy(s2); + assertThat(f.sort).isEqualTo(s1.and(s2)); + } +}