在HqlNamedParams中,如果where条件没有产生结果,则获取所有行

时间:2013-08-27 09:49:11

标签: hql

StringBuilder query = new StringBuilder("from country ");
query.append("and stateId in (:stateId)");
hqlNamedParam.addParamAndValue("stateId", values);

如果表中不存在“values”,则获取所有行。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

我误解了这个问题,现在我编辑了我的答案:

您必须运行两个查询。

// The first:

StringBuilder query = new StringBuilder("from country ");
query.append("and stateId in (:stateId)");
hqlNamedParam.addParamAndValue("stateId", values);

// The second (only if the first doesn't return rows)

if (outList == null || outList.size == 0) {
    StringBuilder query2 = new StringBuilder("from country ");
}

或者(但你使用更多带有计数的子查询)

StringBuilder query = new StringBuilder("from country ");
query.append(" where 
    (select count(c2.id) from country c2 where c2.stateid in (:stateId)) > 0
    and cs.stateid in (:stateId)
    or (select count(c3.id) from country c3 where c3.stateid in (:stateId)) = 0");
hqlNamedParam.addParamAndValue("stateId", values);