DATAJPA-456 - Add support for specifying only the projection part of a custom count query.

We now support to specify just the projection part for the count query generation via the "countProjection" property of the @Query annotation.
This avoids having to repetitively specify parts of the original query.

Original pull request: #84.
This commit is contained in:
Thomas Darimont
2014-04-29 01:23:01 +02:00
committed by Oliver Gierke
parent d4acfeb0fa
commit 122d9c5ea2
8 changed files with 106 additions and 13 deletions

View File

@@ -1393,6 +1393,34 @@ public class UserRepositoryTests {
assertThat(result, is(data));
}
/**
* @see DATAJPA-456
*/
@Test
public void findPaginatedExplicitQueryWithCountQueryProjection() {
firstUser.setFirstname(null);
flushTestUsers();
Page<User> result = repository.findAllByFirstnameLike("", new PageRequest(0, 10));
assertThat(result.getContent().size(), is(3));
}
/**
* @see DATAJPA-456
*/
@Test
public void findPaginatedNamedQueryWithCountQueryProjection() {
flushTestUsers();
Page<User> result = repository.findByNamedQueryAndCountProjection("Gierke", new PageRequest(0, 10));
assertThat(result.getContent().size(), is(1));
}
private Page<User> executeSpecWithSort(Sort sort) {
flushTestUsers();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2008-2013 the original author or authors.
* Copyright 2008-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -272,6 +272,15 @@ public class QueryUtilsUnitTests {
assertCountQuery("select p.lastname,p.firstname from Person p", "select count(p) from Person p");
}
/**
* @see DATAJPA-456
*/
@Test
public void createCountQueryFromTheGivenCountProjection() {
assertThat(createCountQueryFor("select p.lastname,p.firstname from Person p", "p.lastname"),
is("select count(p.lastname) from Person p"));
}
private void assertCountQuery(String originalQuery, String countQuery) {
assertThat(createCountQueryFor(originalQuery), is(countQuery));
}

View File

@@ -368,4 +368,16 @@ public interface UserRepository extends JpaRepository<User, Integer>, JpaSpecifi
*/
@Procedure
Integer plus1(@Param("arg") Integer arg);
/**
* @see DATAJPA-456
*/
@Query(value = "select u from User u where u.firstname like ?1%", countProjection = "u.firstname")
Page<User> findAllByFirstnameLike(String firstname, Pageable page);
/**
* @see DATAJPA-456
*/
@Query(name = "User.findBySpringDataNamedQuery", countProjection = "u.firstname")
Page<User> findByNamedQueryAndCountProjection(String firstname, Pageable page);
}