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 67c9b2a98..f16e660a6 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 @@ -294,7 +294,18 @@ public class SimpleJpaRepository implements JpaRepos return Collections.emptyList(); } - ByIdsSpecification specification = new ByIdsSpecification(); + if (entityInformation.hasCompositeId()) { + + List results = new ArrayList(); + + for (ID id : ids) { + results.add(findOne(id)); + } + + return results; + } + + ByIdsSpecification specification = new ByIdsSpecification(entityInformation); TypedQuery query = getQuery(specification, (Sort) null); return query.setParameter(specification.parameter, ids).getResultList(); @@ -564,10 +575,16 @@ public class SimpleJpaRepository implements JpaRepos * @author Oliver Gierke */ @SuppressWarnings("rawtypes") - private final class ByIdsSpecification implements Specification { + private static final class ByIdsSpecification implements Specification { + + private final JpaEntityInformation entityInformation; ParameterExpression parameter; + public ByIdsSpecification(JpaEntityInformation entityInformation) { + this.entityInformation = entityInformation; + } + /* * (non-Javadoc) * @see org.springframework.data.jpa.domain.Specification#toPredicate(javax.persistence.criteria.Root, javax.persistence.criteria.CriteriaQuery, javax.persistence.criteria.CriteriaBuilder) diff --git a/src/test/java/org/springframework/data/jpa/repository/RepositoryWithCompositeKeyTests.java b/src/test/java/org/springframework/data/jpa/repository/RepositoryWithCompositeKeyTests.java index 2871859cf..758ad751e 100644 --- a/src/test/java/org/springframework/data/jpa/repository/RepositoryWithCompositeKeyTests.java +++ b/src/test/java/org/springframework/data/jpa/repository/RepositoryWithCompositeKeyTests.java @@ -18,6 +18,7 @@ package org.springframework.data.jpa.repository; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; +import java.util.Arrays; import java.util.List; import org.junit.Rule; @@ -263,4 +264,41 @@ public class RepositoryWithCompositeKeyTests { assertThat(employeeRepositoryWithEmbeddedId.exists(key), is(true)); } + + /** + * @see DATAJPA-611 + */ + @Test + public void shouldAllowFindAllWithIdsForEntitiesWithCompoundIdClassKeys() { + + IdClassExampleDepartment dep2 = new IdClassExampleDepartment(); + dep2.setDepartmentId(2L); + dep2.setName("Dep2"); + + IdClassExampleEmployee emp1 = new IdClassExampleEmployee(); + emp1.setEmpId(3L); + emp1.setDepartment(dep2); + emp1 = employeeRepositoryWithIdClass.save(emp1); + + IdClassExampleDepartment dep1 = new IdClassExampleDepartment(); + dep1.setDepartmentId(1L); + dep1.setName("Dep1"); + + IdClassExampleEmployee emp2 = new IdClassExampleEmployee(); + emp2.setEmpId(2L); + emp2.setDepartment(dep1); + emp2 = employeeRepositoryWithIdClass.save(emp2); + + IdClassExampleEmployeePK emp1PK = new IdClassExampleEmployeePK(); + emp1PK.setDepartment(2L); + emp1PK.setEmpId(3L); + + IdClassExampleEmployeePK emp2PK = new IdClassExampleEmployeePK(); + emp1PK.setDepartment(1L); + emp1PK.setEmpId(2L); + + List result = employeeRepositoryWithIdClass.findAll(Arrays.asList(emp1PK, emp2PK)); + + assertThat(result, hasSize(2)); + } }