DATAJPA-332 - Optimization for SimpleJpaRepository.findAll(…) taking Iterable.

We now shortcut the execution of SimpleJpaRepository.findAll(Iterable<Integer> ids) to return an empty collection in case no ids or null are given. Previously building a query with an IN clause failed with an empty parameter list given.
This commit is contained in:
Oliver Gierke
2013-04-19 10:29:52 +02:00
parent 4b1cd49079
commit edddb6dfb5
2 changed files with 16 additions and 1 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2008-2012 the original author or authors.
* Copyright 2008-2013 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.
@@ -253,6 +253,10 @@ public class SimpleJpaRepository<T, ID extends Serializable> implements JpaRepos
*/
public List<T> findAll(Iterable<ID> ids) {
if (ids == null || !ids.iterator().hasNext()) {
return Collections.emptyList();
}
return getQuery(new Specification<T>() {
public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
Path<?> path = root.get(entityInformation.getIdAttribute());

View File

@@ -24,6 +24,7 @@ import static org.springframework.data.jpa.domain.sample.UserSpecifications.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@@ -1049,6 +1050,16 @@ public class UserRepositoryTests {
assertThat(repository.countUsersByFirstname("Dave"), is(1));
}
/**
* @see DATAJPA-332
*/
@Test
public void findAllReturnsEmptyIterableIfNoIdsGiven() {
assertThat(repository.findAll(Collections.<Integer> emptySet()), is(emptyIterable()));
assertThat(repository.findAll((Iterable<Integer>) null), is(emptyIterable()));
}
private Page<User> executeSpecWithSort(Sort sort) {
flushTestUsers();