Skip to content

Commit 7498dfb

Browse files
committed
Recognize RIGHT, LEFT, OUTER, INNER and FULL as reserved words in HQL parser.
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 dde3fe1 commit 7498dfb

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
@@ -793,6 +793,121 @@ void sortProperlyAppendsToExistingOrderByWithFunction() {
793793
"select e from SampleEntity e where function('nativeFunc', ?1) > 'testVal' order by function('nativeFunc', ?1), e.age desc");
794794
}
795795

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

0 commit comments

Comments
 (0)