Skip to content

Commit 37ca755

Browse files
mp911degregturn
authored andcommitted
Fix NullPointer when deriving a count query for non-SELECT statements.
Closes #2812
1 parent 4dd9369 commit 37ca755

File tree

4 files changed

+21
-5
lines changed

4 files changed

+21
-5
lines changed

src/main/java/org/springframework/data/jpa/repository/query/DeclaredQuery.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ default boolean usesPaging() {
105105

106106
/**
107107
* Return whether the query is a native query of not.
108-
*
108+
*
109109
* @return <code>true</code> if native query otherwise <code>false</code>
110110
*/
111111
default boolean isNativeQuery() {

src/main/java/org/springframework/data/jpa/repository/query/QueryUtils.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ static String createCountQueryFor(String originalQuery, @Nullable String countPr
604604

605605
String replacement = useVariable ? SIMPLE_COUNT_VALUE : complexCountValue;
606606

607-
if (nativeQuery && (variable.contains(",") || "*".equals(variable))) {
607+
if (variable != null && (nativeQuery && (variable.contains(",") || "*".equals(variable)))) {
608608
replacement = "1";
609609
} else {
610610

src/test/java/org/springframework/data/jpa/repository/query/DefaultQueryUtilsUnitTests.java

+9
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,15 @@ void createsCountQueryForDistinctQueries() {
7070
"select count(distinct u) from User u where u.foo = ?");
7171
}
7272

73+
@Test // GH-2812
74+
void createsCountQueryForDeleteQuery() {
75+
76+
String result = createCountQueryFor("delete from some_table where id in :ids", null, true);
77+
78+
// ح(•̀ж•́)ง †
79+
assertThat(result).isEqualTo("deleteselect count(where) from some_table where id in :ids");
80+
}
81+
7382
@Test
7483
void createsCountQueryForConstructorQueries() {
7584

src/test/java/org/springframework/data/jpa/repository/query/QueryEnhancerUnitTests.java

+10-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@
1515
*/
1616
package org.springframework.data.jpa.repository.query;
1717

18-
import static org.assertj.core.api.Assertions.assertThat;
19-
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
20-
import static org.assertj.core.api.Assertions.assertThatThrownBy;
18+
import static org.assertj.core.api.Assertions.*;
2119

2220
import java.util.Arrays;
2321
import java.util.Collections;
@@ -236,6 +234,15 @@ void createsCountQueryForNativeScalarSelects() {
236234
assertCountQuery("select p.lastname,p.firstname from Person p", "select count(1) from Person p", true);
237235
}
238236

237+
@Test // GH-2812
238+
void createCountQueryFromDeleteQuery() {
239+
240+
StringQuery query = new StringQuery("delete from some_table where id in :ids", true);
241+
242+
assertThat(getEnhancer(query).createCountQueryFor("p.lastname"))
243+
.isEqualToIgnoringCase("delete from some_table where id in :ids");
244+
}
245+
239246
@Test // DATAJPA-456
240247
void createCountQueryFromTheGivenCountProjection() {
241248

0 commit comments

Comments
 (0)