我正在构建一个查询,我想将集合作为参数传递:
List<String> items = new LinkedList();
//adding optional items
Query query = s.getNamedQuery("myQueryName").setParameterList("item", items);
我的查询如下所示:
SELECT i from Item i
//... not important things
WHERE ( i.name in (:item) )
但我想让它成为可选的,所以项目列表可能是空的。但是当它为空时,我得到一个异常:
org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected end of subtree
所以我尝试使用一些功能,例如:
SELECT i from Item i
//... not important things
WHERE ( (:item) is null or i.name in (:item) )
SELECT i from Item i
//... not important things
WHERE ( (:item) is empty or i.name in (:item) )
SELECT i from Item i
//... not important things
WHERE ( size(:item)=0 or i.name in (:item) )
但它似乎都不起作用。如何解决此问题并仅在列表不为空时检查项目名称是否在列表中?