DATAJPA-231 - Added support for derived countBy queries.
PartTreeJpaQuery evaluates the newly introduced PartTree.isCountProjection() to rather trigger a derived count query instead of the actual query. Extended Query execution to potentially massage the returned value into the return type of the query method to allow long and int as return types for count queries.
This commit is contained in:
@@ -1027,6 +1027,28 @@ public class UserRepositoryTests {
|
||||
assertThat(result, hasItem(thirdUser));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAJPA-231
|
||||
*/
|
||||
@Test
|
||||
public void executesDerivedCountQueryToLong() {
|
||||
|
||||
flushTestUsers();
|
||||
|
||||
assertThat(repository.countByLastname("Matthews"), is(1L));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAJPA-231
|
||||
*/
|
||||
@Test
|
||||
public void executesDerivedCountQueryToInt() {
|
||||
|
||||
flushTestUsers();
|
||||
|
||||
assertThat(repository.countUsersByFirstname("Dave"), is(1));
|
||||
}
|
||||
|
||||
private Page<User> executeSpecWithSort(Sort sort) {
|
||||
|
||||
flushTestUsers();
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2008-2011 the original author or authors.
|
||||
* Copyright 2008-2013 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.
|
||||
@@ -88,6 +88,7 @@ public class JpaQueryExecutionUnitTests {
|
||||
when(query.executeUpdate()).thenReturn(0);
|
||||
when(method.getReturnType()).thenReturn((Class) void.class);
|
||||
when(jpaQuery.createQuery(Mockito.any(Object[].class))).thenReturn(query);
|
||||
when(jpaQuery.getQueryMethod()).thenReturn(method);
|
||||
|
||||
ModifyingExecution execution = new ModifyingExecution(method, em);
|
||||
execution.execute(jpaQuery, new Object[] {});
|
||||
|
||||
@@ -254,4 +254,14 @@ public interface UserRepository extends JpaRepository<User, Integer>, JpaSpecifi
|
||||
|
||||
@Query(value = "SELECT 1 FROM User", nativeQuery = true)
|
||||
List<Integer> findOnesByNativeQuery();
|
||||
|
||||
/**
|
||||
* @see DATAJPA-231
|
||||
*/
|
||||
long countByLastname(String lastname);
|
||||
|
||||
/**
|
||||
* @see DATAJPA-231
|
||||
*/
|
||||
int countUsersByFirstname(String firstname);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user