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

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

问题描述

我有一个像这样的 HQL 语句:

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)

然而,有时 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.

如果您的输入列表是 typeNamesOrig:

If your input list is typeNamesOrig:

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

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

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