Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate DetachedCriteria中的析取问题_Hibernate_Criteria - Fatal编程技术网

Hibernate DetachedCriteria中的析取问题

Hibernate DetachedCriteria中的析取问题,hibernate,criteria,Hibernate,Criteria,我将得到三个列表和两个操作符op1,op2,它们可以是“或”,“和”,“和不”。。我需要像这样形成一个查询 select emp_num , emp_dpt , emp_name from employee where emp_id in (list1) and emp_id in (list2) and emp_id in (list3) 例如,考虑一个表雇员,我将得到三个列表的List1、List2、List3的EMPUID ID < /

我将得到三个列表和两个操作符op1,op2,它们可以是“或”,“和”,“和不”。。我需要像这样形成一个查询

select emp_num , emp_dpt , emp_name

from employee

where
    emp_id in (list1)

    and

    emp_id in (list2)

    and

    emp_id in (list3)

例如,考虑一个表雇员,我将得到三个列表的List1、List2、List3的EMPUID ID < /P>

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;
    }
}
但我想要的是

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在哪里,我都期待着或它正在放置


需要一些帮助…

来自
dc.add()
。每个add都将是一个标准,与and组合在一起。带1个参数的析取基本上是一个noop,因为
crit和
=
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));
    }
}
public List getRequestedEmployee(列表列表1、列表列表2、列表列表3、字符串op1、字符串op2)
{
ICriterion crit=null;
如果(!CollectionUtils.isEmpty(列表1))
{
crit=限制。in(“emp_id”,列表1));
}
如果(!CollectionUtils.isEmpty(列表3)
{
crit=添加标准(crit,列表2,op1);
}
如果(!CollectionUtils.isEmpty(列表3)
{
crit=addCriteria(crit,列表3,op2);
}
DetachedCriteria查询=DetachedCriteria.forClass(员工);
如果(临界值!=null)
{
query=query.add(crit);
}
返回查询;
}
私有ICriterion addCriteria(ICriterion existingCrit、列表empIdList、字符串运算符)
{
if(CollectionUtils.IsEmpty(empIdList))
{
返回现有crit;
}
ICriterion crit=限制。in(“emp_Id”,empIdList);
if(existingCrit==null)
{
返回暴击;
}
else if(“and.”等于(运算符))
{
返回限制。连接()
.添加(现有CRIT);
.添加(crit);
}
else if(“或“.equals(运算符))
{
返回限制。析取()
.添加(现有CRIT);
.添加(crit);
}
else/“not”
{
返回限制。连接()
.添加(现有CRIT);
.添加(限制,而不是(临界值));
}
}