Skip to content

Commit 0f8279b

Browse files
committed
Properly handle reserved words as entity names.
In JPQL and HQL, we need to properly handle reserved words that crop up as entity names (which is legal). See #2982.
1 parent ad7b280 commit 0f8279b

File tree

5 files changed

+43
-12
lines changed

5 files changed

+43
-12
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,7 @@ reservedWord
713713
| FETCH
714714
| FILTER
715715
| FIRST
716+
| FLOOR
716717
| FOLLOWING
717718
| FOR
718719
| FORMAT

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -596,12 +596,13 @@ trim_character
596596

597597
identification_variable
598598
: IDENTIFICATION_VARIABLE
599-
| ORDER // Gap in the spec requires supporting 'Order' as an entity name
600-
| COUNT // Gap in the spec requires supporting 'count' as a possible name
601-
| KEY // Gap in the sepc requires supported 'key' as a possible name
602-
| LEFT
599+
| f=(COUNT
603600
| INNER
601+
| KEY
602+
| LEFT
603+
| ORDER
604604
| OUTER
605+
| FLOOR)
605606
;
606607

607608
constructor_name
@@ -682,8 +683,7 @@ collection_value_field
682683
;
683684

684685
entity_name
685-
: identification_variable
686-
| identification_variable ('.' identification_variable)* // Hibernate sometimes expands the entity name to FQDN when using named queries
686+
: identification_variable ('.' identification_variable)* // Hibernate sometimes expands the entity name to FQDN when using named queries
687687
;
688688

689689
result_variable

Diff for: spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/query/JpqlQueryRenderer.java

+2-6
Original file line numberDiff line numberDiff line change
@@ -2118,12 +2118,8 @@ public List<JpaQueryParsingToken> visitIdentification_variable(JpqlParser.Identi
21182118

21192119
if (ctx.IDENTIFICATION_VARIABLE() != null) {
21202120
return List.of(new JpaQueryParsingToken(ctx.IDENTIFICATION_VARIABLE()));
2121-
} else if (ctx.COUNT() != null) {
2122-
return List.of(new JpaQueryParsingToken(ctx.COUNT()));
2123-
} else if (ctx.ORDER() != null) {
2124-
return List.of(new JpaQueryParsingToken(ctx.ORDER()));
2125-
} else if (ctx.KEY() != null) {
2126-
return List.of(new JpaQueryParsingToken(ctx.KEY()));
2121+
} else if (ctx.f != null) {
2122+
return List.of(new JpaQueryParsingToken(ctx.f));
21272123
} else {
21282124
return List.of();
21292125
}

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

+17
Original file line numberDiff line numberDiff line change
@@ -1499,4 +1499,21 @@ WITH maxId AS(select max(sr.snapshot.id) snapshotId from SnapshotReference sr
14991499
select sr from maxId m join SnapshotReference sr on sr.snapshot.id = m.snapshotId
15001500
""");
15011501
}
1502+
1503+
@Test // GH-2982
1504+
void floorShouldBeValidEntityName() {
1505+
1506+
assertQuery("""
1507+
SELECT f
1508+
FROM Floor f
1509+
WHERE f.name = :name
1510+
""");
1511+
1512+
assertQuery("""
1513+
SELECT r
1514+
FROM Room r
1515+
JOIN r.floor f
1516+
WHERE f.name = :name
1517+
""");
1518+
}
15021519
}

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

+17
Original file line numberDiff line numberDiff line change
@@ -914,4 +914,21 @@ void theRest38() {
914914
WHERE l.product.name = ?1
915915
""");
916916
}
917+
918+
@Test // GH-2982
919+
void floorShouldBeValidEntityName() {
920+
921+
assertQuery("""
922+
SELECT f
923+
FROM Floor f
924+
WHERE f.name = :name
925+
""");
926+
927+
assertQuery("""
928+
SELECT r
929+
FROM Room r
930+
JOIN r.floor f
931+
WHERE f.name = :name
932+
""");
933+
}
917934
}

0 commit comments

Comments
 (0)