Skip to content

Commit e1fa886

Browse files
committed
Handle subqueries when used inside HQL INSERT statements.
Potentialy subqueries can crop up inside INSERT statements. We need to handle this as well. NOTE: INSERT statements aren't defined in JPQL, so this problem doesn't overlap with standard JPA queries. See #2977
1 parent 93e4025 commit e1fa886

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

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

+2
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ private static boolean isSubquery(ParserRuleContext ctx) {
9696
return true;
9797
} else if (ctx instanceof HqlParser.SelectStatementContext) {
9898
return false;
99+
} else if (ctx instanceof HqlParser.InsertStatementContext) {
100+
return false;
99101
} else {
100102
return isSubquery(ctx.getParent());
101103
}

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

+13
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,19 @@ void fromWithoutAPrimaryAliasShouldWork() {
973973
.isEqualTo("FROM Story WHERE enabled = true order by created desc");
974974
}
975975

976+
@Test // GH-2977
977+
void isSubqueryThrowsException() {
978+
979+
String query = """
980+
insert into MyEntity (id, col)
981+
select max(id), col
982+
from MyEntityStaging
983+
group by col
984+
""";
985+
986+
assertThat(createQueryFor(query, Sort.unsorted())).isEqualToIgnoringWhitespace(query);
987+
}
988+
976989
private void assertCountQuery(String originalQuery, String countQuery) {
977990
assertThat(createCountQueryFor(originalQuery)).isEqualTo(countQuery);
978991
}

0 commit comments

Comments
 (0)