From 3257368f002e4bc2c3c6ccebdd7c831fbb1938aa Mon Sep 17 00:00:00 2001 From: "Greg L. Turnquist" Date: Tue, 23 May 2023 08:21:27 -0500 Subject: [PATCH] Support alternative NOT EQUALS operators in HQL. Hibernate also supports "!=" and "^=" as NOT EQUALS operators. See #2970 --- .../data/jpa/repository/query/Hql.g4 | 3 ++- .../repository/query/HqlQueryRendererTests.java | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/spring-data-jpa/src/main/antlr4/org/springframework/data/jpa/repository/query/Hql.g4 b/spring-data-jpa/src/main/antlr4/org/springframework/data/jpa/repository/query/Hql.g4 index 8796cf1f82..f02e46084a 100644 --- a/spring-data-jpa/src/main/antlr4/org/springframework/data/jpa/repository/query/Hql.g4 +++ b/spring-data-jpa/src/main/antlr4/org/springframework/data/jpa/repository/query/Hql.g4 @@ -533,8 +533,9 @@ expressionOrPredicate ; // https://docs.jboss.org/hibernate/orm/6.1/userguide/html_single/Hibernate_User_Guide.html#hql-relational-comparisons +// NOTE: The TIP shows that "!=" is also supported. Hibernate's source code shows that "^=" is another NOT_EQUALS option as well. relationalExpression - : expression op=('=' | '>' | '>=' | '<' | '<=' | '<>' ) expression + : expression op=('=' | '>' | '>=' | '<' | '<=' | '<>' | '!=' | '^=' ) expression ; // https://docs.jboss.org/hibernate/orm/6.1/userguide/html_single/Hibernate_User_Guide.html#hql-between-predicate diff --git a/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/query/HqlQueryRendererTests.java b/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/query/HqlQueryRendererTests.java index f6343f51bd..6cb62adae3 100644 --- a/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/query/HqlQueryRendererTests.java +++ b/spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/query/HqlQueryRendererTests.java @@ -564,6 +564,22 @@ WHERE TYPE(e) <> Exempt """); } + @Test // GH-2970 + void alternateNotEqualsShouldAlsoWork() { + + assertQuery(""" + SELECT TYPE(e) + FROM Employee e + WHERE TYPE(e) != Exempt + """); + + assertQuery(""" + SELECT TYPE(e) + FROM Employee e + WHERE TYPE(e) ^= Exempt + """); + } + @Test void theRest5() {