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 9f4390002..62bf23371 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 @@ -25,9 +25,11 @@ import java.util.List; import javax.persistence.EntityManager; import javax.persistence.LockModeType; import javax.persistence.NoResultException; +import javax.persistence.Parameter; import javax.persistence.TypedQuery; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.ParameterExpression; import javax.persistence.criteria.Path; import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Root; @@ -272,12 +274,10 @@ public class SimpleJpaRepository implements JpaRepos return Collections.emptyList(); } - return getQuery(new Specification() { - public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) { - Path path = root.get(entityInformation.getIdAttribute()); - return path.in(cb.parameter(Iterable.class, "ids")); - } - }, (Sort) null).setParameter("ids", ids).getResultList(); + ByIdsSpecification specification = new ByIdsSpecification(); + TypedQuery query = getQuery(specification, (Sort) null); + + return query.setParameter(specification.parameter, ids).getResultList(); } /* @@ -524,4 +524,29 @@ public class SimpleJpaRepository implements JpaRepos LockModeType type = lockMetadataProvider == null ? null : lockMetadataProvider.getLockModeType(); return type == null ? query : query.setLockMode(type); } + + /** + * Specification that gives access to the {@link Parameter} instance used to bind the ids for + * {@link SimpleJpaRepository#findAll(Iterable)}. Workaround for OpenJPA not binding collections to in-clauses + * correctly when using by-name binding. + * + * @see https://issues.apache.org/jira/browse/OPENJPA-2018?focusedCommentId=13924055 + * @author Oliver Gierke + */ + @SuppressWarnings("rawtypes") + private final class ByIdsSpecification implements Specification { + + ParameterExpression parameter; + + /* + * (non-Javadoc) + * @see org.springframework.data.jpa.domain.Specification#toPredicate(javax.persistence.criteria.Root, javax.persistence.criteria.CriteriaQuery, javax.persistence.criteria.CriteriaBuilder) + */ + public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuilder cb) { + + Path path = root.get(entityInformation.getIdAttribute()); + parameter = cb.parameter(Iterable.class); + return path.in(parameter); + } + } }