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 82a5a59a6..94150da04 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 @@ -28,6 +28,7 @@ import javax.persistence.NoResultException; import javax.persistence.TypedQuery; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Path; import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Root; @@ -231,6 +232,20 @@ public class SimpleJpaRepository implements JpaRepos return getQuery(null, (Sort) null).getResultList(); } + /* + * (non-Javadoc) + * @see org.springframework.data.repository.CrudRepository#findAll(ID[]) + */ + public List findAll(Iterable ids) { + + return getQuery(new Specification() { + public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) { + Path path = root.get(entityInformation.getIdAttribute()); + return path.in(cb.parameter(List.class, "ids")); + } + }, (Sort) null).setParameter("ids", ids).getResultList(); + } + /* * (non-Javadoc) * @see org.springframework.data.jpa.repository.JpaRepository#findAll(org.springframework.data.domain.Sort) diff --git a/src/test/java/org/springframework/data/jpa/repository/EclipseLinkNamespaceUserRepositoryTests.java b/src/test/java/org/springframework/data/jpa/repository/EclipseLinkNamespaceUserRepositoryTests.java index fb22096a5..748dc8e77 100644 --- a/src/test/java/org/springframework/data/jpa/repository/EclipseLinkNamespaceUserRepositoryTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/EclipseLinkNamespaceUserRepositoryTests.java @@ -28,4 +28,11 @@ import org.springframework.test.context.ContextConfiguration; @ContextConfiguration(value = "classpath:eclipselink.xml", inheritLocations = true) public class EclipseLinkNamespaceUserRepositoryTests extends NamespaceUserRepositoryTests { + /** + * Ignored until https://bugs.eclipse.org/bugs/show_bug.cgi?id=349477 is resolved. + */ + @Override + public void findsAllByGivenIds() { + + } } diff --git a/src/test/java/org/springframework/data/jpa/repository/OpenJpaNamespaceUserRepositoryTests.java b/src/test/java/org/springframework/data/jpa/repository/OpenJpaNamespaceUserRepositoryTests.java index fc249b810..394029bdb 100644 --- a/src/test/java/org/springframework/data/jpa/repository/OpenJpaNamespaceUserRepositoryTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/OpenJpaNamespaceUserRepositoryTests.java @@ -15,6 +15,24 @@ */ package org.springframework.data.jpa.repository; +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + +import java.util.Arrays; +import java.util.Collection; +import java.util.List; + +import javax.persistence.EntityManager; +import javax.persistence.Parameter; +import javax.persistence.PersistenceContext; +import javax.persistence.TypedQuery; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Root; + +import org.junit.Ignore; +import org.junit.Test; +import org.springframework.data.jpa.domain.sample.User; import org.springframework.data.jpa.repository.sample.UserRepository; import org.springframework.test.context.ContextConfiguration; @@ -26,4 +44,40 @@ import org.springframework.test.context.ContextConfiguration; @ContextConfiguration(value = "classpath:openjpa.xml", inheritLocations = true) public class OpenJpaNamespaceUserRepositoryTests extends NamespaceUserRepositoryTests { + @PersistenceContext + EntityManager em; + + /** + * Ignored until https://issues.apache.org/jira/browse/OPENJPA-2018 gets fixed. + */ + @Override + @Ignore + public void findsAllByGivenIds() { + + } + + /** + * Test case for https://issues.apache.org/jira/browse/OPENJPA-2018 + */ + @SuppressWarnings({ "rawtypes", "unchecked" }) + @Test + @Ignore + public void queryUsingIn() { + + flushTestUsers(); + + CriteriaBuilder builder = em.getCriteriaBuilder(); + + CriteriaQuery criteriaQuery = builder.createQuery(User.class); + Root root = criteriaQuery.from(User.class); + criteriaQuery.where(root. get("id").in(builder.parameter(Collection.class))); + + TypedQuery query = em.createQuery(criteriaQuery); + for (Parameter parameter : query.getParameters()) { + query.setParameter(parameter, Arrays.asList(1, 2)); + } + + List resultList = query.getResultList(); + assertThat(resultList.size(), is(2)); + } } \ No newline at end of file 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 105cd2a13..711a8ce6e 100644 --- a/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/UserRepositoryTests.java @@ -106,6 +106,15 @@ public class UserRepositoryTests { assertThat(firstUser.getFirstname(), is(foundPerson.getFirstname())); } + @Test + public void findsAllByGivenIds() { + + flushTestUsers(); + + Iterable result = repository.findAll(Arrays.asList(firstUser.getId(), secondUser.getId())); + assertThat(result, hasItems(firstUser, secondUser)); + } + @Test public void testReadByIdReturnsNullForNotFoundEntities() { @@ -726,7 +735,7 @@ public class UserRepositoryTests { assertThat(result, hasItem(firstUser)); } - private void flushTestUsers() { + protected void flushTestUsers() { firstUser = repository.save(firstUser); secondUser = repository.save(secondUser);