Skip to content

Commit 490d34a

Browse files
christophstroblmp911de
authored andcommitted
Fix NPE when parsing modifying HQL.
Closes #3649 Original pull request: #3650
1 parent ae2aa63 commit 490d34a

File tree

2 files changed

+21
-12
lines changed

2 files changed

+21
-12
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,12 @@ static boolean isSubquery(ParserRuleContext ctx) {
4848
return false;
4949
} else if (ctx instanceof HqlParser.InsertStatementContext) {
5050
return false;
51+
} else if (ctx instanceof HqlParser.DeleteStatementContext) {
52+
return false;
53+
} else if (ctx instanceof HqlParser.UpdateStatementContext) {
54+
return false;
5155
} else {
52-
return isSubquery(ctx.getParent());
56+
return ctx.getParent() != null && isSubquery(ctx.getParent());
5357
}
5458
}
5559

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

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,16 +1027,22 @@ void fromWithoutAPrimaryAliasShouldWork() {
10271027
.isEqualTo("FROM Story WHERE enabled = true order by created desc");
10281028
}
10291029

1030-
@Test // GH-2977
1031-
void isSubqueryThrowsException() {
1032-
1033-
String query = """
1034-
insert into MyEntity (id, col)
1035-
select max(id), col
1036-
from MyEntityStaging
1037-
group by col
1038-
""";
1039-
1030+
@ParameterizedTest
1031+
@ValueSource(strings = { """
1032+
insert into MyEntity (id, col)
1033+
select max(id), col
1034+
from MyEntityStaging
1035+
group by col
1036+
""", """
1037+
update MyEntity AS mes
1038+
set mes.col = 'test'
1039+
where mes.id = 1
1040+
""", """
1041+
delete MyEntity AS mes
1042+
where mes.col = 'test'
1043+
"""
1044+
}) // GH-2977, GH-3649
1045+
void isSubqueryThrowsException(String query) {
10401046
assertThat(createQueryFor(query, Sort.unsorted())).isEqualToIgnoringWhitespace(query);
10411047
}
10421048

@@ -1133,7 +1139,6 @@ void sortShouldBeAppendedToSubSelectWithSetOperatorInSubselect(String alias) {
11331139
}
11341140
assertThat(count).describedAs("Found order by clause more than once in: \n%s", it).isOne();
11351141
});
1136-
11371142
}
11381143

11391144
private void assertCountQuery(String originalQuery, String countQuery) {

0 commit comments

Comments
 (0)