Skip to content

Commit 5593bb6

Browse files
committed
Enable KeyValuePartTreeQuery usage for projections.
Closes #563
1 parent 00cf874 commit 5593bb6

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

src/main/java/org/springframework/data/keyvalue/repository/query/KeyValuePartTreeQuery.java

+3-5
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,13 @@ protected Object doExecute(Object[] parameters, KeyValueQuery<?> query) {
134134
} else if (queryMethod.isCollectionQuery()) {
135135

136136
return this.keyValueOperations.find(query, queryMethod.getEntityInformation().getJavaType());
137-
} else if (queryMethod.isQueryForEntity()) {
137+
} else if (partTree.get().isExistsProjection()) {
138+
return keyValueOperations.exists(query, queryMethod.getEntityInformation().getJavaType());
139+
} else {
138140

139141
Iterable<?> result = this.keyValueOperations.find(query, queryMethod.getEntityInformation().getJavaType());
140142
return result.iterator().hasNext() ? result.iterator().next() : null;
141-
} else if (partTree.get().isExistsProjection()) {
142-
return keyValueOperations.exists(query, queryMethod.getEntityInformation().getJavaType());
143143
}
144-
145-
throw new UnsupportedOperationException("Query method not supported");
146144
}
147145

148146
protected KeyValueQuery<?> prepareQuery(Object[] parameters) {

src/test/java/org/springframework/data/keyvalue/repository/query/KeyValuePartTreeQueryUnitTests.java

+27
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,27 @@ void shouldApplyPageableParameterToCollectionQuery() throws SecurityException, N
9393
assertThat(query.getRows()).isEqualTo(3);
9494
}
9595

96+
@Test // GH-563
97+
@SuppressWarnings({ "unchecked", "rawtypes" })
98+
void shouldAllowProjectionQueries() throws SecurityException, NoSuchMethodException {
99+
100+
when(metadataMock.getDomainType()).thenReturn((Class) Person.class);
101+
when(metadataMock.getDomainTypeInformation()).thenReturn((TypeInformation) TypeInformation.of(Person.class));
102+
when(metadataMock.getReturnType(any(Method.class))).thenReturn((TypeInformation) TypeInformation.of(List.class));
103+
when(metadataMock.getReturnedDomainClass(any(Method.class))).thenReturn((Class) Person.class);
104+
105+
QueryMethod qm = new QueryMethod(Repo.class.getMethod("findProjectionByFirstname",String.class), metadataMock,
106+
projectionFactoryMock);
107+
108+
KeyValuePartTreeQuery partTreeQuery = new KeyValuePartTreeQuery(qm, QueryMethodEvaluationContextProvider.DEFAULT,
109+
kvOpsMock, SpelQueryCreator.class);
110+
111+
KeyValueQuery<?> query = partTreeQuery.prepareQuery(new Object[] { "firstname" });
112+
partTreeQuery.doExecute(new Object[] { "firstname" }, query);
113+
114+
verify(kvOpsMock).find(eq(query), eq(Person.class));
115+
}
116+
96117
@Test // DATAKV-142
97118
@SuppressWarnings({ "unchecked", "rawtypes" })
98119
void shouldApplyDerivedMaxResultsToQuery() throws SecurityException, NoSuchMethodException {
@@ -166,5 +187,11 @@ interface Repo {
166187
List<Person> findTop3By();
167188

168189
List<Person> findTop3ByFirstname(String firstname);
190+
191+
PersonProjection findProjectionByFirstname(String firstname);
192+
}
193+
194+
interface PersonProjection {
195+
String getFirstname();
169196
}
170197
}

0 commit comments

Comments
 (0)