我正在使用JPA 2.1.2。
我想用where子句执行select查询。 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
当我从不等于(!=)变为等于(=)时,查询工作正常。
那么如何使用不相等的枚举比较?
答案 0 :(得分:37)
不相等? <>
使用此运算符。
JPQL运算符(根据章节10.2.5.6。 JPQL Language Reference)
答案 1 :(得分:1)
使用Jpql时,&#34;不等于&#34; 的正确运算符为&lt;&gt; 。所以,像这样更新您的代码:
return em.createQuery("SELECT c FROM MyEntity c WHERE c.reminderStage <> :reminderStage", MyEntity.class)
.setParameter("reminderStage", stage).getResultList();
删除!= 使用&lt;&gt; 。