Skip to content

Commit eb80b8c

Browse files
committed
simplified forQueryByIds
1 parent bf27503 commit eb80b8c

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/convert/SqlParametersFactory.java

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.ArrayList;
2020
import java.util.List;
2121
import java.util.Map;
22+
import java.util.function.BiFunction;
2223
import java.util.function.Function;
2324
import java.util.function.Predicate;
2425

@@ -148,31 +149,27 @@ <T> SqlIdentifierParameterSource forQueryByIds(Iterable<?> ids, Class<T> domainT
148149

149150
SqlIdentifierParameterSource parameterSource = new SqlIdentifierParameterSource();
150151

151-
RelationalPersistentEntity<?> entity = context.getPersistentEntity(domainType);
152+
RelationalPersistentEntity<?> entity = context.getRequiredPersistentEntity(domainType);
152153
RelationalPersistentProperty singleIdProperty = entity.getRequiredIdProperty();
154+
RelationalPersistentEntity<?> complexId = context.getPersistentEntity(singleIdProperty);
155+
AggregatePath.ColumnInfos idColumnInfos = context.getAggregatePath(entity).getTableInfo().idColumnInfos();
153156

154-
if (singleIdProperty.isEntity()) {
155-
156-
RelationalPersistentEntity<?> complexId = context.getPersistentEntity(singleIdProperty);
157-
158-
AggregatePath.ColumnInfos idColumnInfos = context.getAggregatePath(entity).getTableInfo().idColumnInfos();
157+
BiFunction<Object, AggregatePath, Object> valueExtractor = complexId == null
158+
? (id, ap) -> id
159+
: (id, ap) -> complexId.getPropertyPathAccessor(id).getProperty(ap.getRequiredPersistentPropertyPath());
159160

160-
List<Object[]> parameterValues = new ArrayList<>();
161-
for (Object id : ids) {
161+
List<Object[]> parameterValues = new ArrayList<>();
162+
for (Object id : ids) {
162163

163-
PersistentPropertyPathAccessor<Object> accessor = complexId.getPropertyPathAccessor(id);
164+
List<Object> tupleList = new ArrayList<>();
165+
idColumnInfos.forEach((ap, ci) -> {
166+
tupleList.add(valueExtractor.apply(id, ap));
167+
});
168+
parameterValues.add(tupleList.toArray(new Object[0]));
169+
}
164170

165-
List<Object> tupleList = new ArrayList<>();
166-
idColumnInfos.forEach((ap, ci) -> {
167-
tupleList.add(accessor.getProperty(ap.getRequiredPersistentPropertyPath()));
168-
});
169-
parameterValues.add(tupleList.toArray(new Object[0]));
170-
}
171+
parameterSource.addValue(SqlGenerator.IDS_SQL_PARAMETER, parameterValues);
171172

172-
parameterSource.addValue(SqlGenerator.IDS_SQL_PARAMETER, parameterValues);
173-
} else {
174-
addConvertedPropertyValuesAsList(parameterSource, getRequiredPersistentEntity(domainType).getRequiredIdProperty(), ids);
175-
}
176173
return parameterSource;
177174
}
178175

0 commit comments

Comments
 (0)