DATACMNS-128 - Implement CrudRepository.findAll(Iterable<ID> ids).
Implemented said method in SimpleJpaRepository and added test case. Comment that one out for failing persistence providers. https://bugs.eclipse.org/bugs/show_bug.cgi?id=349477 https://issues.apache.org/jira/browse/OPENJPA-2018
This commit is contained in:
@@ -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<T, ID extends Serializable> implements JpaRepos
|
||||
return getQuery(null, (Sort) null).getResultList();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.CrudRepository#findAll(ID[])
|
||||
*/
|
||||
public List<T> findAll(Iterable<ID> ids) {
|
||||
|
||||
return getQuery(new Specification<T>() {
|
||||
public Predicate toPredicate(Root<T> 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)
|
||||
|
||||
@@ -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() {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<User> criteriaQuery = builder.createQuery(User.class);
|
||||
Root<User> root = criteriaQuery.from(User.class);
|
||||
criteriaQuery.where(root.<Integer> get("id").in(builder.parameter(Collection.class)));
|
||||
|
||||
TypedQuery<User> query = em.createQuery(criteriaQuery);
|
||||
for (Parameter parameter : query.getParameters()) {
|
||||
query.setParameter(parameter, Arrays.asList(1, 2));
|
||||
}
|
||||
|
||||
List<User> resultList = query.getResultList();
|
||||
assertThat(resultList.size(), is(2));
|
||||
}
|
||||
}
|
||||
@@ -106,6 +106,15 @@ public class UserRepositoryTests {
|
||||
assertThat(firstUser.getFirstname(), is(foundPerson.getFirstname()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void findsAllByGivenIds() {
|
||||
|
||||
flushTestUsers();
|
||||
|
||||
Iterable<User> 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);
|
||||
|
||||
Reference in New Issue
Block a user