Restrictons.in(..) generates invalid SQL if list of values is empty

Description

I have classes called A and B. There is a 1:n relationship from A to B.

Class B has the following in its mapping file:

<many-to-one name="a" class="A" column="fk_a_id" not-null="true" />

If I write the following code:

List<B> foo() {
List<A> aList = ....
Session session = ....
Criteria query = session.createCriteria(B.class);
query.add(Restrictions.in("a", aList));
return query.list();
}

Then all works fine and the results I want are returned. However, if aList is empty (i.e. aList.size()==0) then the query.list function generates and executes invalid SQL for MySQL.

java.sql.SQLException: Syntax error or access violation message from server: "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1"

I imagine that it is generating SQL like IN () which isn't valid.

I realize it's a bit ridiculous to want to find the rows where a field is IN the empty list. Nothing will ever be returned. However, in my opinion it should still be allowed, and return no rows, just as saying "WHERE 1=2" is allowed, just doesn't return any rows.

Activity

Steve EbersoleOctober 30, 2015 at 11:48 AM

Agreed. Same discussion on HHH-8091.

Christian BeikovOctober 30, 2015 at 10:45 AM

I think this is a duplicate. Only difference is that this issue is concerned with the Criteria API

Łukasz StrzeleckiSeptember 19, 2014 at 11:22 AM

It would be nice to have supoprt ofr NullObject like empty list.

I always need to write specific test to remember about this 'special case'.

simar mugattarovJanuary 24, 2014 at 6:41 AM

Same error in sql using postgresql 9.2 and hibernate 4.2 final sp1. In my case i manually check length of collection and add null if size is equal 0

PetrPDecember 6, 2012 at 10:07 AM

I think nhibernate goes right way by 0=1 Are you going to backport this into hibernate?
Thank you.

Duplicate

Details

Assignee

Reporter

Labels

Components

Priority

Created August 9, 2007 at 6:49 PM
Updated October 30, 2015 at 11:48 AM
Resolved October 30, 2015 at 11:48 AM