Hibernate Criteria在OR中具有一组条件,从列表中挑选

时间:2016-07-23 01:27:04

标签: hibernate orm

我在SellingRate实体中有一个属性“salePointId”。现在我想创建一个Criteria,以便在列表中使用salePointId获取对象。

    List<Integer> salePointIds; // some list coming from code above
    Criteria criteria = session.createCriteria(SellingRate.class)
            .add(Restrictions.ge("endDate", date))
            .add(Restrictions.le("startDate", date));
    for (int id: salePointIds) {
        // what should be written here
        // to get an OR of restrictions on id
    }

1 个答案:

答案 0 :(得分:0)

想出来。可以使用Restriocion.disjunction()。

List<Integer> salePointIds; // some list coming from code above
Criteria criteria = session.createCriteria(SellingRate.class)
        .add(Restrictions.ge("endDate", date))
        .add(Restrictions.le("startDate", date));
Disjunction conditionsInOR = Restrictions.disjunction();
for (int id: salePointIds) {
    conditionsInOR.add(Restrictions.eq("id", id));
}
criteria.add(conditionsInOR);

替代方案:

Critera criteria = session.createCriteria(SellingRate.class)
        .add(Restrictions.ge("endDate", date)
        .add(Restrictions.le("startDate", date)
        .add(Property.forName("id").in(list.toArray())