Skip to content

Commit fa17008

Browse files
christophstroblmp911de
authored andcommitted
Fix NPE when parsing modifying HQL.
Closes #3649 Original pull request: #3650
1 parent 9c6dd1f commit fa17008

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,12 @@ private static boolean isSubquery(ParserRuleContext ctx) {
101101
return false;
102102
} else if (ctx instanceof HqlParser.InsertStatementContext) {
103103
return false;
104+
} else if (ctx instanceof HqlParser.DeleteStatementContext) {
105+
return false;
106+
} else if (ctx instanceof HqlParser.UpdateStatementContext) {
107+
return false;
104108
} else {
105-
return isSubquery(ctx.getParent());
109+
return ctx.getParent() != null ? isSubquery(ctx.getParent()) : false;
106110
}
107111
}
108112

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

+17-10
Original file line numberDiff line numberDiff line change
@@ -995,16 +995,23 @@ void fromWithoutAPrimaryAliasShouldWork() {
995995
.isEqualTo("FROM Story WHERE enabled = true order by created desc");
996996
}
997997

998-
@Test // GH-2977
999-
void isSubqueryThrowsException() {
1000-
1001-
String query = """
1002-
insert into MyEntity (id, col)
1003-
select max(id), col
1004-
from MyEntityStaging
1005-
group by col
1006-
""";
1007-
998+
@ParameterizedTest
999+
@ValueSource(strings = { """
1000+
insert into MyEntity (id, col)
1001+
select max(id), col
1002+
from MyEntityStaging
1003+
group by col
1004+
""", """
1005+
update MyEntity AS mes
1006+
set mes.col = 'test'
1007+
where mes.id = 1
1008+
""", """
1009+
delete MyEntity AS mes
1010+
where mes.col = 'test'
1011+
"""
1012+
1013+
}) // GH-2977, GH-3649
1014+
void isSubqueryThrowsException(String query) {
10081015
assertThat(createQueryFor(query, Sort.unsorted())).isEqualToIgnoringWhitespace(query);
10091016
}
10101017

0 commit comments

Comments
 (0)