Skip to content

Commit 718cfa8

Browse files
committed
Recognize RIGHT, LEFT, OUTER, INNER and FULL as HQL reserved words.
RIGHT, LEFT, OUTER, INNER, and FULL are HQL tokens. This means they also need to be recognized as potential reserved words and thus possibly identifiers. This can show up if someone, for example, uses "right" as the name of a relationship in a JPA entity. Resolves #2864.
1 parent eb3920d commit 718cfa8

File tree

2 files changed

+120
-5
lines changed

2 files changed

+120
-5
lines changed

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -700,7 +700,7 @@ reservedWord
700700
| FOR
701701
| FORMAT
702702
| FROM
703-
// | FULL
703+
| FULL
704704
| FUNCTION
705705
| GROUP
706706
| GROUPS
@@ -712,7 +712,7 @@ reservedWord
712712
| IN
713713
| INDEX
714714
| INDICES
715-
// | INNER
715+
| INNER
716716
| INSERT
717717
| INSTANT
718718
| INTERSECT
@@ -722,7 +722,7 @@ reservedWord
722722
| KEY
723723
| LAST
724724
| LEADING
725-
// | LEFT
725+
| LEFT
726726
| LIKE
727727
| LIMIT
728728
| LIST
@@ -760,7 +760,7 @@ reservedWord
760760
| OR
761761
| ORDER
762762
| OTHERS
763-
// | OUTER
763+
| OUTER
764764
| OVER
765765
| OVERFLOW
766766
| OVERLAY
@@ -773,7 +773,7 @@ reservedWord
773773
| QUARTER
774774
| RANGE
775775
| RESPECT
776-
// | RIGHT
776+
| RIGHT
777777
| ROLLUP
778778
| ROW
779779
| ROWS

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

+115
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,121 @@ void countQueryShouldWorkEvenWithoutExplicitAlias() {
808808
"select count(b) FROM BookError b WHERE portal = :portal");
809809
}
810810

811+
@Test // GH-2864
812+
void usingRightAsARelationshipNameShouldWork() {
813+
814+
HqlQueryParser.parseQuery("""
815+
select u
816+
from UserAccountEntity u
817+
join fetch u.lossInspectorLimitConfiguration lil
818+
join fetch u.companyTeam ct
819+
where exists (
820+
select iu
821+
from UserAccountEntity iu
822+
join iu.roles u2r
823+
join u2r.role r
824+
join r.rights r2r
825+
join r2r.right rt
826+
where
827+
rt.code = :rightCode
828+
and iu = u
829+
)
830+
and ct.id = :teamId
831+
""");
832+
}
833+
834+
@Test // GH-2864
835+
void usingLeftAsARelationshipNameShouldWork() {
836+
837+
HqlQueryParser.parseQuery("""
838+
select u
839+
from UserAccountEntity u
840+
join fetch u.lossInspectorLimitConfiguration lil
841+
join fetch u.companyTeam ct
842+
where exists (
843+
select iu
844+
from UserAccountEntity iu
845+
join iu.roles u2r
846+
join u2r.role r
847+
join r.rights r2r
848+
join r2r.left lt
849+
where
850+
lt.code = :rightCode
851+
and iu = u
852+
)
853+
and ct.id = :teamId
854+
""");
855+
}
856+
857+
@Test // GH-2864
858+
void usingOuterAsARelationshipNameShouldWork() {
859+
860+
HqlQueryParser.parseQuery("""
861+
select u
862+
from UserAccountEntity u
863+
join fetch u.lossInspectorLimitConfiguration lil
864+
join fetch u.companyTeam ct
865+
where exists (
866+
select iu
867+
from UserAccountEntity iu
868+
join iu.roles u2r
869+
join u2r.role r
870+
join r.rights r2r
871+
join r2r.outer ou
872+
where
873+
ou.code = :rightCode
874+
and iu = u
875+
)
876+
and ct.id = :teamId
877+
""");
878+
}
879+
880+
@Test // GH-2864
881+
void usingFullAsARelationshipNameShouldWork() {
882+
883+
HqlQueryParser.parseQuery("""
884+
select u
885+
from UserAccountEntity u
886+
join fetch u.lossInspectorLimitConfiguration lil
887+
join fetch u.companyTeam ct
888+
where exists (
889+
select iu
890+
from UserAccountEntity iu
891+
join iu.roles u2r
892+
join u2r.role r
893+
join r.rights r2r
894+
join r2r.full fu
895+
where
896+
fu.code = :rightCode
897+
and iu = u
898+
)
899+
and ct.id = :teamId
900+
""");
901+
}
902+
903+
@Test // GH-2864
904+
void usingInnerAsARelationshipNameShouldWork() {
905+
906+
HqlQueryParser.parseQuery("""
907+
select u
908+
from UserAccountEntity u
909+
join fetch u.lossInspectorLimitConfiguration lil
910+
join fetch u.companyTeam ct
911+
where exists (
912+
select iu
913+
from UserAccountEntity iu
914+
join iu.roles u2r
915+
join u2r.role r
916+
join r.rights r2r
917+
join r2r.inner in
918+
where
919+
in.code = :rightCode
920+
and iu = u
921+
)
922+
and ct.id = :teamId
923+
""");
924+
}
925+
811926
private void assertCountQuery(String originalQuery, String countQuery) {
812927
assertThat(createCountQueryFor(originalQuery)).isEqualTo(countQuery);
813928
}

0 commit comments

Comments
 (0)