Skip to content

Commit 053a462

Browse files
committed
Consider only top-level properties for tuple query selection.
We now only consider top-level properties for tuple query selection to avoid join products caused by selecting nested relationships. Closes #3908
1 parent 5a22fbf commit 053a462

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

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

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@
3737
import java.util.Collection;
3838
import java.util.Collections;
3939
import java.util.HashMap;
40+
import java.util.HashSet;
4041
import java.util.List;
4142
import java.util.Map;
4243
import java.util.Optional;
44+
import java.util.Set;
4345
import java.util.function.BiConsumer;
4446
import java.util.function.Function;
4547

@@ -817,11 +819,18 @@ private <S extends T> TypedQuery<S> getQuery(ReturnedType returnedType, @Nullabl
817819
}
818820

819821
List<Selection<?>> selections = new ArrayList<>();
820-
822+
Set<String> topLevelProperties = new HashSet<>();
821823
for (String property : requiredSelection) {
822824

823-
PropertyPath path = PropertyPath.from(property, returnedType.getDomainType());
824-
selections.add(QueryUtils.toExpressionRecursively(root, path, true).alias(property));
825+
int separator = property.indexOf('.');
826+
String topLevelProperty = separator == -1 ? property : property.substring(0, separator);
827+
828+
if (!topLevelProperties.add(topLevelProperty)) {
829+
continue;
830+
}
831+
832+
PropertyPath path = PropertyPath.from(topLevelProperty, returnedType.getDomainType());
833+
selections.add(QueryUtils.toExpressionRecursively(root, path, true).alias(topLevelProperty));
825834
}
826835

827836
Class<?> typeToRead = returnedType.getReturnedType();

0 commit comments

Comments
 (0)