正确的表达式不是 JPQL/HQL 查询中的有效表达式

The right expression is not a valid expression in the JPQL/HQL query

我不明白为什么我的 JPQL 表达式是错误的。我想获取未从我的数据库中删除且目前正在租用的自行车的列表。你能帮帮我吗?

@Data
@MappedSuperclass
public abstract class AbstractEntity implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @Basic(optional = false)
    @Column(nullable = false)
    private boolean isRemoved;
}

@Data
@Entity
@NamedQueries({
        @NamedQuery(name = "Bike.findRent", query = "SELECT b FROM Bike b WHERE NOT b.isRemoved AND b.isRent")
})
public class Bike extends AbstractEntity {

    @Basic(optional = false)
    @Column(nullable = false)
    private boolean isRent;
}

@Repository
public class BikeDao extends BaseDao<Bike> {

    public BikeDao() {
        super(Bike.class);
    }

    public List<Bike> findRent() {
        return em.createNamedQuery("Bike.findRent", Bike.class).getResultList();
    }
}

在我 运行 我的 Spring 应用程序并通过 Postman 尝试 post 自行车数据之后。但是,

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

这是什么意思?我找不到问题所在。

根据 JPA 规范(4.5 WHERE 子句):

A WHERE clause is defined as follows:

where_clause ::= WHERE conditional_expression

并且在 conditional expression 中,您只能使用可用的简单表达式的组合,例如比较、between、in、like、null 比较(也称为 null)、空集合、集合成员、exist 表达式。

因此,您应该按以下方式更正您的查询:

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