WHERE b.maxOrdini > maxordine 抛出 "The identification variable 'maxordine' is not defined in the FROM clause"

WHERE b.maxOrdini > maxordine throws "The identification variable 'maxordine' is not defined in the FROM clause"

我试图在我的 Java EE 应用程序中编写此查询:

public List<Fornitore> findByMaxOrdine(int maxordine) 
{
    Query query;
    query = em.createQuery("SELECT b FROM Fornitore b WHERE b.maxOrdini > maxordine");
    query.setParameter("maxordine", maxordine);
    return query.getResultList();
}

当我尝试 运行 结果是这样的:

Exception Description: Problem compiling [SELECT b FROM Fornitore b WHERE b.maxOrdini > maxordine][46, 55] The identification variable 'maxordine' cannot be used in conjunction with the > operator.[46, 55] The identification variable 'maxordine' is not defined in the FROM clause.

我不明白为什么它不起作用。 感谢您的帮助。

由于 maxordine 是命名参数,因此您的查询应该是:

"SELECT b FROM Fornitore b WHERE b.maxOrdini > :maxordine"

如果愿意,您也可以使用位置参数而不是命名参数。然后,(重构的)方法看起来像:

public List<Fornitore> findByMaxOrdine(int maxordine) {
    return em.createQuery("SELECT b FROM Fornitore b WHERE b.maxOrdini > ?1")
           .setParameter(1, maxordine);
           .getResultList();
}