Skip to content

Commit c63d0bf

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

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

Diff for: 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() {

Diff for: 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

Diff for: 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

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

+9
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,15 @@ void findsExistingOrderByIndependentOfCase() {
172172
assertThat(query).endsWithIgnoringCase("ORDER BY p.firstname, p.lastname asc");
173173
}
174174

175+
@Test // GH-2812
176+
void createCountQueryFromDeleteQuery() {
177+
178+
StringQuery query = new StringQuery("delete from some_table where id in :ids", true);
179+
180+
assertThat(getEnhancer(query).createCountQueryFor("p.lastname"))
181+
.isEqualToIgnoringCase("delete from some_table where id in :ids");
182+
}
183+
175184
@Test // DATAJPA-456
176185
void createCountQueryFromTheGivenCountProjection() {
177186

0 commit comments

Comments
 (0)