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:
@@ -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);
|
||||
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user