Anton
Anton

Reputation: 1079

Criteria with "in" operator and empty list

I have next problem.

I have next criteria:

criteria.add(Restrictions.in("entity.otherEntity", getOtherEntitiesList()));

if my otherEntitiesList is empty. Hibernate generate next query:

select count(*) as y0_ 
from OTHER_ENTITY this_ 
where this_.OTHER_ENTITY_ID in ( ) 

and i get exception from Oracle.

I found next line in Hibernate code:

 String params = values.length > 0 ? StringHelper.repeat(
                singleValueParam + ", ", values.length - 1 )
                + singleValueParam : "";

It's not possible use this method to generate criteria. I need a way to generate Hibernate criteria with empty list. If list is empty I want to get empty answer.

Is it possible?

Thanks

Upvotes: 1

Views: 1622

Answers (1)

Konstantin Yovkov
Konstantin Yovkov

Reputation: 62864

Why don't you check the list size first and then create a restriction (if the size is > 0):

if (getOtherEntitiesList() != null && getOtherEntitiesList().size() > 0) {
    criteria.add(Restrictions.in("entity.otherEntity", getOtherEntitiesList()));
}

Upvotes: 2

Related Questions