我有一个像这样的HQL语句:
Select 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: unexpected end of subtree [Select cast(ed.employee.employeeID as int) AS emp_id FROM Education AS ed WHERE ed.type.name IN ()]
接受空列表的解决方案是什么?
解决方法
我使用的一个解决方案是将一些虚拟值与输入一起放在列表中,以确保它永远不会为空.当然,只有选择虚拟值才能实现.
如果您的输入列表是typeNamesOrig:
List<String> typeNames = new ArrayList<String>(typeNamesOrig); typeNames.add("valueThatDoesNotExistForSure"); query.setParameterList("typeNames",typeNames);