Skip to content

Commit 298c1c6

Browse files
committed
Polishing.
Consistently convert Iterable to Collection for identifier parameters. See #3580
1 parent f7aed2a commit 298c1c6

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

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

+8-11
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,6 @@
4040
import java.util.Optional;
4141
import java.util.function.BiConsumer;
4242
import java.util.function.Function;
43-
import java.util.stream.Collectors;
44-
import java.util.stream.StreamSupport;
4543

4644
import org.springframework.data.domain.Example;
4745
import org.springframework.data.domain.KeysetScrollPosition;
@@ -246,14 +244,8 @@ public void deleteAllByIdInBatch(Iterable<ID> ids) {
246244
/*
247245
* Some JPA providers require {@code ids} to be a {@link Collection} so we must convert if it's not already.
248246
*/
249-
250-
if (ids instanceof Collection) {
251-
query.setParameter("ids", ids);
252-
} else {
253-
Collection<ID> idsCollection = StreamSupport.stream(ids.spliterator(), false)
254-
.collect(Collectors.toCollection(ArrayList::new));
255-
query.setParameter("ids", idsCollection);
256-
}
247+
Collection<ID> idCollection = toCollection(ids);
248+
query.setParameter("ids", idCollection);
257249

258250
applyQueryHints(query);
259251

@@ -414,7 +406,7 @@ public List<T> findAllById(Iterable<ID> ids) {
414406
return results;
415407
}
416408

417-
Collection<ID> idCollection = Streamable.of(ids).toList();
409+
Collection<ID> idCollection = toCollection(ids);
418410

419411
ByIdsSpecification<T> specification = new ByIdsSpecification<>(entityInformation);
420412
TypedQuery<T> query = getQuery(specification, Sort.unsorted());
@@ -918,6 +910,11 @@ private ProjectionFactory getProjectionFactory() {
918910
return projectionFactory;
919911
}
920912

913+
@SuppressWarnings({ "rawtypes", "unchecked" })
914+
private static <T> Collection<T> toCollection(Iterable<T> ids) {
915+
return ids instanceof Collection c ? c : Streamable.of(ids).toList();
916+
}
917+
921918
/**
922919
* Executes a count query and transparently sums up all values returned.
923920
*

0 commit comments

Comments
 (0)