正确的表达式不是jpql/hql查询中的有效表达式

q3qa4bjr  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(476)

我不明白为什么我的jpql表达式是错误的。我想得到没有从我的数据库中删除的自行车列表,目前正在租赁。你能帮助我吗?

  1. @Data
  2. @MappedSuperclass
  3. public abstract class AbstractEntity implements Serializable {
  4. @Id
  5. @GeneratedValue(strategy = GenerationType.IDENTITY)
  6. private Integer id;
  7. @Basic(optional = false)
  8. @Column(nullable = false)
  9. private boolean isRemoved;
  10. }
  11. @Data
  12. @Entity
  13. @NamedQueries({
  14. @NamedQuery(name = "Bike.findRent", query = "SELECT b FROM Bike b WHERE NOT b.isRemoved AND b.isRent")
  15. })
  16. public class Bike extends AbstractEntity {
  17. @Basic(optional = false)
  18. @Column(nullable = false)
  19. private boolean isRent;
  20. }
  21. @Repository
  22. public class BikeDao extends BaseDao<Bike> {
  23. public BikeDao() {
  24. super(Bike.class);
  25. }
  26. public List<Bike> findRent() {
  27. return em.createNamedQuery("Bike.findRent", Bike.class).getResultList();
  28. }
  29. }

在我运行我的spring应用程序并通过postman尝试发布自行车数据之后。但是,

  1. Caused by: org.eclipse.persistence.exceptions.EntityManagerSetupException:
  2. Exception Description: Deployment of PersistenceUnit [default] failed. Close all factories for this PersistenceUnit.
  3. Internal Exception: Exception [EclipseLink-0] (Eclipse Persistence Services - 2.7.7.v20200504-69f2c2b80d): org.eclipse.persistence.exceptions.JPQLException
  4. Exception Description: Syntax error parsing [SELECT b FROM Bike b WHERE NOT b.isRemoved AND b.isRent].
  5. [47, 55] The right expression is not a valid expression.

这是什么意思?我查不出哪里出了问题。

t3psigkw

t3psigkw1#

根据jpa规范(4.5 where条款):
where子句定义如下: where_clause ::= WHERE conditional_expression 在条件表达式中,只能使用可用简单表达式的组合,例如comparison、between、in、like、null comparison(aka is null)、empty collection、collection member和exist表达式。
因此,您应该按照以下方式更正您的查询:

  1. SELECT b FROM Bike b WHERE b.isRemoved = false AND b.isRent = true

相关问题