2014-01-06 38 views
0

我有下一个问题。带“in”运算符和空列表的条件

我有未来的标准:

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

如果我的otherEntitiesList是空的。休眠生成下一个查询:

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

和我从甲骨文得到例外。

我发现在Hibernate中下一行代码:

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

这是不可能使用这种方法生成的标准。 我需要一种方式来生成空列表的Hibernate标准。如果列表为空,我希望得到空的答案。

可能吗?

感谢

+0

如果该列表是空的,不应该有一个结果,所以只需返回一个空列表而不进行查询。 –

回答

2

你为什么不检查列表的大小,然后再创建一个限制(如大小为> 0):

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

或者只是在没有数据库命中的情况下返回零。 –

+0

我需要不返回任何内容的条件。 但是我不想写类似criteria.add(Restrictions.ne(“Id”, - 1L))。add(Restrictions.eq(“Id”, - 1L))) – Anton

+0

在您的示例中,条件将返回所有行从表中。 – Anton