DetachedCriteria中的Disjunction问题

时间:2012-03-28 18:42:38

标签: hibernate criteria

我将获得三个列表和两个运算符op1,op2可以是“OR”,“AND”,“AND NOT”..我需要形成这样的查询

例如考虑一个表雇员,我将获得三个列表List1,list2,emp3的list3

select emp_num , emp_dpt , emp_name

from employee

where
    emp_id in (list1)

    op1

    emp_id in (list2)

    op2

    emp_id in (list3)

我写了两个方法

public List<Employee> getRequestedEmployee(List<Long> list1 , List<Long> list2, List<Long> list3 , String op1 , String op2) {

    DetachedCriteria dc = DetachedCriteria.forClass(Employee)

    if (!CollectionUtils.isEmpty(list1))
    {
        dc.add(Restrictions.in(list1,emp_id));
    }

    if(!CollectionUtils.isEmpty(list3)
    {
        dc = getCriteria(dc , list2 , op1);
    }

    if(!CollectionUtils.isEmpty(list3)
    {
        dc = getCriteria(dc , list3 , op2);
    }
}

private DetachedCriteria getCriteria( DetachedCriteria dc , List<long> empIdList, String operator)
{
    if(!CollectionUtils.IsEmpty(empIdList))
    {
        if("and".equals(operator))
        {
            Conjunction andOp = Restrictions.Conjunction();
            andOp.add(Restrictions.in(empIdList));
            dc.add(andOp);
        }
        else if("or".equals(operator))
        {
            Disjunction orOp = restrictions.disjunction();
            orOp.add(Restrictions.in(empIdList))
            dc.add(andOp);
        }
        else
        {
             dc.add(Restrictions.not(empIdList));
        }
        return dc;
    }
}

我面临Disjunction的问题..当传递OR时,它正在解释为AND ..

例如,如果我正在传递op1 = andop2 = or正在形成这样的

select emp_num , emp_dpt , emp_name

from employee

where
    emp_id in (list1)

    and

    emp_id in (list2)

    and

    emp_id in (list3)

但我想要的是

select emp_num , emp_dpt , emp_name

from employee

where
    emp_id in (list1)

    and

    emp_id in (list2)

    or

    emp_id in (list3)

无论是op1还是op2

,无论是期待还是放置

需要一些帮助...

1 个答案:

答案 0 :(得分:0)

And来自dc.add()。每个添加将是一个标准,与和放在一起。与1参数分离基本上是一个noop,因为crit and <nothing> = crit。您需要在标准级别上累积标准,而不是在查询/标准级别

public List<Employee> getRequestedEmployee(List<Long> list1 , List<Long> list2, List<Long> list3 , String op1 , String op2)
{
    ICriterion crit = null;

    if (!CollectionUtils.isEmpty(list1))
    {
        crit = Restrictions.in("emp_id", list1));
    }

    if(!CollectionUtils.isEmpty(list3)
    {
        crit = addCriteria(crit , list2 , op1);
    }

    if(!CollectionUtils.isEmpty(list3)
    {
        crit = addCriteria(crit , list3 , op2);
    }

    DetachedCriteria query = DetachedCriteria.forClass(Employee);
    if (crit != null)
    {
        query = query.add(crit);
    }
    return query;
}

private ICriterion addCriteria( ICriterion existingCrit, List<long> empIdList, String operator)
{
    if(CollectionUtils.IsEmpty(empIdList))
    {
        return existingCrit;
    }

    ICriterion crit = Restrictions.in("emp_Id", empIdList);

    if(existingCrit == null)
    {
        return crit;
    }
    else if("and".equals(operator))
    {
        return Restrictions.conjunction()
            .add(existingCrit);
            .add(crit);
    }
    else if("or".equals(operator))
    {
        return Restrictions.disjunction()
            .add(existingCrit);
            .add(crit);
    }
    else // "not"
    {
        return Restrictions.conjunction()
            .add(existingCrit);
            .add(Restrictions.not(crit));
    }
}