Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HQL Where IN for empty list crashes

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?

like image 844
Derek Avatar asked Dec 09 '11 19:12

Derek


3 Answers

You can set :typeNames list as null if array is empty.

if(typeNames.isEmpty()) typeNames = null

// Call Query

like image 135
Dayoung Avatar answered Nov 04 '22 16:11

Dayoung


If typeNames is empty/null, I probably wouldn't execute the query:

if (typeNames) result = Foo.executeQuery("select ... where e.type.name in :typeNames", [typeNames: typeNames)
like image 15
Ted Naleid Avatar answered Nov 04 '22 16:11

Ted Naleid


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);
like image 5
Alex Gitelman Avatar answered Nov 04 '22 17:11

Alex Gitelman