JPA 枚举比较不等于失败

2022-09-01 21:45:40

我使用的是 JPA 2.1.2。

我想使用 where 子句执行选择查询。where 语句应比较(不等于)存储在数据库(字符串)中的枚举。

@Entity
@Table(name = "MY_ENTITY")
public class MyEntity implements Serializable {

    @Column(name = "REMINDER_STATE")
    @Enumerated(EnumType.STRING)
    private ReminderStage reminderStage;

    ...
}

class DaoImpl{
 ....

    @Override
    public List<MyEntity> findAll(ReminderStage stage) {
        return em.createQuery("SELECT c FROM MyEntity c  WHERE  c.reminderStage != :reminderStage", MyEntity.class)
                .setParameter("reminderStage", stage).getResultList();

    }
}

但是当我执行查询时,我得到以下异常:

...
[28.07.14 08:27:07:910 CEST] 0000004e SystemErr     R Caused by: <openjpa-2.1.2-SNAPSHOT-r422266:1530146 nonfatal user error> org.apache.openjpa.persistence.ArgumentException: An error occurred while parsing the query filter 'SELECT c FROM CertInfo c  WHERE  c.reminderStage != :reminderStage'. Error message: org.apache.openjpa.kernel.jpql.TokenMgrError: Lexical error at line 1, column 50.  Encountered: "!" (33), after : ""
[28.07.14 08:27:07:910 CEST] 0000004e SystemErr     R   at org.apache.openjpa.kernel.jpql.JPQLExpressionBuilder$ParsedJPQL.parse(JPQLExpressionBuilder.java:2449)
[28.07.14 08:27:07:910 CEST] 0000004e SystemErr     R   at org.apache.openjpa.kernel.jpql.JPQLExpressionBuilder$ParsedJPQL.<init>(JPQLExpressionBuilder.java:2432)
[28.07.14 08:27:07:910 CEST] 0000004e SystemErr     R   at org.apache.openjpa.kernel.jpql.JPQLParser.parse(JPQLParser.java:49)
[28.07.14 08:27:07:910 CEST] 0000004e SystemErr     R   at org.apache.openjpa.kernel.ExpressionStoreQuery.newCompilation(ExpressionStoreQuery.java:154)
[28.07.14 08:27:07:910 CEST] 0000004e SystemErr     R   at org.apache.openjpa.kernel.QueryImpl.newCompilation(QueryImpl.java:672)
[28.07.14 08:27:07:911 CEST] 0000004e SystemErr     R   at org.apache.openjpa.kernel.QueryImpl.compilationFromCache(QueryImpl.java:654)
[28.07.14 08:27:07:911 CEST] 0000004e SystemErr     R   at org.apache.openjpa.kernel.QueryImpl.compileForCompilation(QueryImpl.java:620)
[28.07.14 08:27:07:911 CEST] 0000004e SystemErr     R   at org.apache.openjpa.kernel.QueryImpl.compileForExecutor(QueryImpl.java:682)
[28.07.14 08:27:07:911 CEST] 0000004e SystemErr     R   at org.apache.openjpa.kernel.QueryImpl.compile(QueryImpl.java:589)
[28.07.14 08:27:07:911 CEST] 0000004e SystemErr     R   at org.apache.openjpa.persistence.EntityManagerImpl.createQuery(EntityManagerImpl.java:996)
[28.07.14 08:27:07:911 CEST] 0000004e SystemErr     R   at com.ibm.ws.persistence.EntityManagerImpl.createQuery(EntityManagerImpl.java:107)
[28.07.14 08:27:07:911 CEST] 0000004e SystemErr     R   at com.ibm.ws.persistence.EntityManagerImpl.createQuery(EntityManagerImpl.java:86)
[28.07.14 08:27:07:911 CEST] 0000004e SystemErr     R   at com.ibm.ws.persistence.EntityManagerImpl.createQuery(EntityManagerImpl.java:34)
[28.07.14 08:27:07:911 CEST] 0000004e SystemErr     R   at org.apache.openjpa.persistence.EntityManagerImpl.createQuery(EntityManagerImpl.java:974)
[28.07.14 08:27:07:911 CEST] 0000004e SystemErr     R   at com.ibm.ws.jpa.management.JPATxEmInvocation.createQuery(JPATxEmInvocation.java:353)
[28.07.14 08:27:07:911 CEST] 0000004e SystemErr     R   at com.ibm.ws.jpa.management.JPAEntityManager.createQuery(JPAEntityManager.java:550)
[28.07.14 08:27:07:911 CEST] 0000004e SystemErr     R   at ...findAll(DaoImpl.java:271)
[28.07.14 08:27:07:911 CEST] 0000004e SystemErr     R   ... 13 more

当我从不等于 (!=) 更改为等于 (=) 时,查询工作正常。

那么我如何使用枚举比较与不等于?


答案 1

不等于 ? 使用此运算符。<>

JPQL 运算符(根据 JPQL 语言参考的第 10.2.5.6 节))

  1. 导航运算符 (.)
  2. 算术运算符:+, - 一元 *, / 乘法和除法 +, - 加法和减法
  3. 比较运算符: =, >, >=, <, <=, <> (不等于), [ NOT ] BETWEEN, [ NOT ] LIKE, [ NOT ] IN, IS [ NOT ] NULL, IS [ NOT ] EMPTY, [ NOT ] MEMBER [ OF ]
  4. 逻辑运算符:NOT AND 或

答案 2

使用 Jpql 时,“不等于”的正确运算符是 <> 。因此,请像这样更新代码:

 return em.createQuery("SELECT c FROM MyEntity c  WHERE  c.reminderStage <> :reminderStage", MyEntity.class)
                .setParameter("reminderStage", stage).getResultList();

删除 !=用<>


推荐