HQL其中IN为空列表崩溃 [英] HQL Where IN for empty list crashes

查看:127
本文介绍了HQL其中IN为空列表崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 选择cast(ed.employee.employeeID as int)AS emp_id FROM Education AS ed WHERE ed.type.name IN(:typeNames)

有时候,typeNames是空的。这会导致以下情况:

  org.hibernate.hql.ast.QuerySyntaxException:子树的意外结束[Select cast(ed.employee .employeeID as int)AS emp_id FROM Education AS ed WHERE ed.type.name IN()] 

什么是解决方案,使接受一个空列表?

解决方案

我使用的一个解决方案,将放置一些虚拟值在列表中与您的输入一起确保它永远不会是空的。如果您的输入列表是 typeNamesOrig :您可以选择虚拟值来做到这一点。

 列表< String> typeNames = new ArrayList< String>(typeNamesOrig); 
typeNames.add(valueThatDoesNotExistForSure);
query.setParameterList(typeNames,typeNames);


I have an HQL statement like so:

Select cast(ed.employee.employeeID as int) AS emp_id FROM Education AS ed WHERE ed.type.name IN (:typeNames)

Sometimes however, typeNames is empty. This causes the following:

org.hibernate.hql.ast.QuerySyntaxException: unexpected end of subtree [Select cast(ed.employee.employeeID as int) AS emp_id FROM Education AS ed WHERE ed.type.name IN ()]

What is the solution to make that accept an empty list?

解决方案

One solution that I used, would be to place some dummy value in the list together with your input to ensure that it's never empty. Of course, you can only do it if dummy value can be chosen.

If your input list is typeNamesOrig:

List<String> typeNames = new ArrayList<String>(typeNamesOrig);
typeNames.add("valueThatDoesNotExistForSure");
query.setParameterList("typeNames",typeNames);

这篇关于HQL其中IN为空列表崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆