DATAJPA-142 - Allow using a NamedQuery for count queries in pagination.

We will now prefer a declared NamedQuery for count queries instead of deriving it from the actual NamedQuery under the following conditions:

- by default a query named ${namedQueryName}.count exists
- the name of the NamedQuery to be used is defined in @Query(countQueryName = "…")

Note that a potential reconfiguration of the NamedQuery name will be taken into account for the according count query name to assure symmetry. Examples

Page<User> findByLastname(String lastname, Pageable pageable)
NamedQuery name: User.findByLastname
Count NamedQuery name: User.findByLastname.count

@Query(name = "Foo.bar")
Page<User> findByLastname(String lastname, Pageable pageable)
NamedQuery name: Foo.bar
Count NamedQuery name: Foo.bar.count

@Query(countName = "Foo.bar.count")
Page<User> findByLastname(String lastname, Pageable pageable)
NamedQuery name: User.findByLastname
Count NamedQuery name: Foo.bar.count

@Query(name = "Foo.bar", countName = "something")
Page<User> findByLastname(String lastname, Pageable pageable)
NamedQuery name: Foo.bar
Count NamedQuery name: something
This commit is contained in:
Oliver Gierke
2012-01-11 15:12:05 +01:00
parent 9224a4b14f
commit 1a14e92e69
5 changed files with 99 additions and 12 deletions

View File

@@ -17,6 +17,7 @@ package org.springframework.data.jpa.repository.query;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import java.lang.reflect.Method;
import java.util.List;
@@ -230,6 +231,29 @@ public class JpaQueryMethodUnitTests {
assertEquals(LockModeType.PESSIMISTIC_WRITE, lockMode);
}
/**
* @see DATAJPA-142
*/
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void returnsDefaultCountQueryName() {
when(metadata.getReturnedDomainClass(repositoryMethod)).thenReturn((Class) User.class);
JpaQueryMethod method = new JpaQueryMethod(repositoryMethod, metadata, extractor);
assertThat(method.getNamedCountQueryName(), is("User.findByLastname.count"));
}
/**
* @see DATAJPA-142
*/
@Test
public void returnsDefaultCountQueryNameBasedOnConfiguredNamedQueryName() {
JpaQueryMethod method = new JpaQueryMethod(namedQuery, metadata, extractor);
assertThat(method.getNamedCountQueryName(), is("Foo.bar.count"));
}
/**
* Interface to define invalid repository methods for testing.
*

View File

@@ -15,6 +15,7 @@
*/
package org.springframework.data.jpa.repository.query;
import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;
import java.lang.reflect.Method;
@@ -61,11 +62,29 @@ public class NamedQueryUnitTests {
public void rejectsPersistenceProviderIfIncapableOfExtractingQueriesAndPagebleBeingUsed() {
when(extractor.canExtractQuery()).thenReturn(false);
JpaQueryMethod queryMethod = new JpaQueryMethod(method, metadata, extractor);
when(em.createNamedQuery(queryMethod.getNamedCountQueryName())).thenThrow(new IllegalArgumentException());
NamedQuery.lookupFrom(queryMethod, em);
}
/**
* @see DATAJPA-142
*/
@Test
public void doesNotRejectPersistenceProviderIfNamedCountQueryIsAvailable() {
when(extractor.canExtractQuery()).thenReturn(false);
JpaQueryMethod queryMethod = new JpaQueryMethod(method, metadata, extractor);
when(em.createNamedQuery(queryMethod.getNamedCountQueryName())).thenReturn(null);
NamedQuery query = (NamedQuery) NamedQuery.lookupFrom(queryMethod, em);
query.doCreateCountQuery(new Object[1]);
verify(em, times(1)).createNamedQuery(queryMethod.getNamedCountQueryName(), Long.class);
verify(em, never()).createQuery(any(String.class), eq(Long.class));
}
interface SampleRepository {
Page<String> foo(Pageable pageable);