From edddb6dfb5fe8b8915da492add2994f87b7994f8 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Fri, 19 Apr 2013 10:29:52 +0200 Subject: [PATCH] =?UTF-8?q?DATAJPA-332=20-=20Optimization=20for=20SimpleJp?= =?UTF-8?q?aRepository.findAll(=E2=80=A6)=20taking=20Iterable.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit We now shortcut the execution of SimpleJpaRepository.findAll(Iterable 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. --- .../jpa/repository/support/SimpleJpaRepository.java | 6 +++++- .../data/jpa/repository/UserRepositoryTests.java | 11 +++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java b/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java index c1a66ec37..f82690e4f 100644 --- a/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java +++ b/src/main/java/org/springframework/data/jpa/repository/support/SimpleJpaRepository.java @@ -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 implements JpaRepos */ public List findAll(Iterable ids) { + if (ids == null || !ids.iterator().hasNext()) { + return Collections.emptyList(); + } + return getQuery(new Specification() { public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) { Path path = root.get(entityInformation.getIdAttribute()); diff --git a/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java b/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java index c2c8cdd39..2b162bfe5 100644 --- a/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java @@ -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. emptySet()), is(emptyIterable())); + assertThat(repository.findAll((Iterable) null), is(emptyIterable())); + } + private Page executeSpecWithSort(Sort sort) { flushTestUsers();