HQL 意外的 AST 节点:{vector} 当使用 ArrayList 作为参数时



搜索并找到了很多关于这个问题的问题,但对于我的具体情况没有任何问题。 我的 HQL 查询出错,代码如下:

String year = "";
ArrayList<String> loc = new ArrayList<>();
// Set above variables to something here
//
query = em.createQuery("select distinct x.fmlName, " +
        "                               x.email " +
        "                   from person x " +
        "                  where x.year = :selYear " +
        "                    and ((:selLoc = 'ALL') " +
        "                        or x.loc IN (:selLoc)) ");
query.setParameter("selYear", year);
query.setParameter("selLoc", loc);
return query.getResultList();

这是错误:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is 
java.lang.IllegalArgumentException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected AST node: {vector}

我对Hibernate相对较新,并且正在努力解决这个问题,因为错误消息不是很有帮助。 有什么想法吗?

你这里有一个问题:

"                    and ((:selLoc = 'ALL') "
//----------------------------^

这应该是字段的名称或字符串,以便:

  • 如果:selLoc应该是字段的名称,则不可能这样做,因为当您使用setPrameter("field", "fieldname")时,您的查询应如下所示'fieldname' = 'ALL'并且不正确,因为字段不应在 2 个引号之间
  • 但似乎您正在使用列表并将其与字符串进行比较,这也是不正确的
  1. (:selLoc)更改为:selLoc。在 HQL 中使用 Java List 时,如下所示:

... WHERE x.loc IN :myList

myList不得在括号中。

  1. 另外,这是错误的(:selLoc = 'ALL')

最新更新