I'll be getting three Lists<Long> and 2 operators op1 , op2 which can be "OR" , "AND", "AND NOT".. i need to form a query like this
for example consider a table employee , i'll be getting three lists List1<long> , list2 , list3 of emp_id's
select emp_num , emp_dpt , emp_name
from employee
where emp_id in (list1) op1 emp_id in (list2)
op2
emp_id in (list3)
I have written two methods
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);//(getCriteria method that is being called here is written down) } if(!CollectionUtils.isEmpty(list3){ dc = getCriteria(dc , list3 , op2);//(getCriteria method that is being called here is written down)
} }
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;
}
I am facing problem with Disjunction .. When ever am passing OR it is interpreting as AND..
for example if am passing op1 = and , op2 = or it is forming like this
select emp_num , emp_dpt , emp_name
from employee
where emp_id in (list1) and emp_id in (list2)
and
emp_id in (list3)
but what i want is
select emp_num , emp_dpt , emp_name
from employee
where emp_id in (list1) and emp_id in (list2)
or
emp_id in (list3)
where ever am expecting or it is putting as and no matter op1 or op2
Need some help...
|