Skip to content

Commit 7c6abe0

Browse files
committed
Skip fenced comments in HQL, EQL and JPQL parsers.
Align with Hibernate and allow comments also in EQL and JPQL. Closes #3997
1 parent 4da017f commit 7c6abe0

File tree

5 files changed

+61
-1
lines changed

5 files changed

+61
-1
lines changed

spring-data-jpa/src/main/antlr4/org/springframework/data/jpa/repository/query/Eql.g4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -859,6 +859,7 @@ reserved_word
859859

860860

861861
WS : [ \t\r\n] -> channel(HIDDEN) ;
862+
COMMENT : '/*' (~'*' | '*' ~'/' )* '*/' -> skip;
862863

863864
// Build up case-insentive tokens
864865

spring-data-jpa/src/main/antlr4/org/springframework/data/jpa/repository/query/Hql.g4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,6 +1561,7 @@ identifier
15611561

15621562

15631563
WS : [ \t\r\n] -> channel(HIDDEN);
1564+
COMMENT : '/*' (~'*' | '*' ~'/' )* '*/' -> skip;
15641565

15651566
// Build up case-insentive tokens
15661567

spring-data-jpa/src/main/antlr4/org/springframework/data/jpa/repository/query/Jpql.g4

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,7 @@ reserved_word
846846

847847

848848
WS : [ \t\r\n] -> channel(HIDDEN) ;
849+
COMMENT : '/*' (~'*' | '*' ~'/' )* '*/' -> skip;
849850

850851
// Build up case-insentive tokens
851852

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ public Set<String> getJoinAliases() {
228228
*/
229229
@Override
230230
public DeclaredQuery getQuery() {
231-
throw new UnsupportedOperationException();
231+
return DeclaredQuery.of(applySorting(Sort.unsorted()), false);
232232
}
233233

234234
/**
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.jpa.repository.query;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import java.util.function.Function;
21+
import java.util.stream.Stream;
22+
23+
import org.junit.jupiter.params.ParameterizedTest;
24+
import org.junit.jupiter.params.provider.MethodSource;
25+
26+
/**
27+
* Unit tests for {@link JpaQueryEnhancer}.
28+
*
29+
* @author Mark Paluch
30+
*/
31+
class JpaQueryEnhancerUnitTests {
32+
33+
@ParameterizedTest // GH-3997
34+
@MethodSource("queryEnhancers")
35+
void shouldRemoveCommentsFromJpql(Function<DeclaredQuery, JpaQueryEnhancer> enhancerFunction) {
36+
37+
QueryEnhancer enhancer = enhancerFunction
38+
.apply(DeclaredQuery.of("SELECT /* foo */ some_alias FROM /* some other */ table_name some_alias", false));
39+
40+
assertThat(enhancer.getQuery().getQueryString())
41+
.isEqualToIgnoringCase("SELECT some_alias FROM table_name some_alias");
42+
43+
enhancer = enhancerFunction.apply(DeclaredQuery.of("""
44+
SELECT /* multi
45+
line
46+
comment
47+
*/ some_alias FROM /* some other */ table_name some_alias
48+
""", false));
49+
50+
assertThat(enhancer.getQuery().getQueryString())
51+
.isEqualToIgnoringCase("SELECT some_alias FROM table_name some_alias");
52+
}
53+
54+
static Stream<Function<DeclaredQuery, JpaQueryEnhancer>> queryEnhancers() {
55+
return Stream.of(JpaQueryEnhancer::forHql, JpaQueryEnhancer::forEql, JpaQueryEnhancer::forJpql);
56+
}
57+
}

0 commit comments

Comments
 (0)