2012-03-28 6 views
0

に私は、3つのリストと2つのオペレータOP1と、「OR」することができOP2を取得「AND」、「AND NOT」..私はこの問題はDetachedCriteriaを

のようなクエリを形成する必要があることでしょう例えば、テーブルの従業員を考慮し、私は二つの方法

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; 
    } 
} 

を書かれている3つのリストのList1、リスト2、EMP_IDの

select emp_num , emp_dpt , emp_name 

from employee 

where 
    emp_id in (list1) 

    op1 

    emp_id in (list2) 

    op2 

    emp_id in (list3) 

のLIST3を取得することがあります。..場合は、これまで私は、論理和との問題に直面しています過ぎ去っているまたはそれは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に関係なく

助けが必要です...

答えて

0

Anddc.add()です。すべての追加は、と一緒に置かれる1つの基準になります。 crit and <nothing> = critなので、1つの議論を伴う論理和は基本的には無駄である。クエリ/基準レベルではない基準レベルの基準を累計する必要があります

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)); 
    } 
}