Skip to content

Commit ff9e081

Browse files
mp911degregturn
authored andcommitted
Fix NullPointer when deriving a count query for non-SELECT statements.
Closes #2812
1 parent 7a9e55f commit ff9e081

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

spring-data-jpa/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() {

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

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

618618
String replacement = useVariable ? SIMPLE_COUNT_VALUE : complexCountValue;
619619

620-
if (nativeQuery && (variable.contains(",") || "*".equals(variable))) {
620+
if (variable != null && (nativeQuery && (variable.contains(",") || "*".equals(variable)))) {
621621
replacement = "1";
622622
} else {
623623

spring-data-jpa/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

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

+9
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,15 @@ void findsExistingOrderByIndependentOfCase() {
176176
endsIgnoringCase(query, "ORDER BY p.firstname, p.lastname asc");
177177
}
178178

179+
@Test // GH-2812
180+
void createCountQueryFromDeleteQuery() {
181+
182+
StringQuery query = new StringQuery("delete from some_table where id in :ids", true);
183+
184+
assertThat(getEnhancer(query).createCountQueryFor("p.lastname"))
185+
.isEqualToIgnoringCase("delete from some_table where id in :ids");
186+
}
187+
179188
@Test // DATAJPA-456
180189
void createCountQueryFromTheGivenCountProjection() {
181190

0 commit comments

Comments
 (0)