DATAJPA-86 - Pagination over queries using group-by works correctly.

Added handling of count methods for queries using group by. In case the count query returns multiple results we use the number of results instead of failing. If the result contains one result we use this one.
This commit is contained in:
Oliver Gierke
2011-09-05 12:05:02 +02:00
parent 1abfa974f6
commit 44e4f1289c
3 changed files with 16 additions and 1 deletions

View File

@@ -15,6 +15,8 @@
*/
package org.springframework.data.jpa.repository.query;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.Query;
@@ -94,7 +96,8 @@ public abstract class JpaQueryExecution {
// Execute query to compute total
Query projection = repositoryQuery.createCountQuery(values);
Long total = (Long) projection.getSingleResult();
List<Long> counts = projection.getResultList();
Long total = counts.size() == 1 ? counts.get(0) : counts.size();
Query query = repositoryQuery.createQuery(values);

View File

@@ -725,6 +725,15 @@ public class UserRepositoryTests {
assertThat(result, hasItem(firstUser));
}
@Test
public void executesPaginationForGroupByQueryCorrectly() {
flushTestUsers();
Page<String> results = repository.findWithGroupBy(new PageRequest(0, 10));
assertThat(results.getTotalPages(), is(1));
}
private Page<User> executeSpecWithSort(Sort sort) {
flushTestUsers();

View File

@@ -178,4 +178,7 @@ public interface UserRepository extends JpaRepository<User, Integer>, JpaSpecifi
List<User> findBySpringDataNamedQuery(String lastname);
Page<User> findByLastnameLike(Pageable pageable, String lastname);
@Query("select u.lastname from User u group by u.lastname")
Page<String> findWithGroupBy(Pageable pageable);
}