Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

hibernate restrictions.in with and, how to use?

I have table like below

id, employee_no, survey_no, name
1    test          1         test_name
2    test2         1         test_name2
3    test3         1         test_name3
4    test4         2         test_name4

how to query with Restriction.in by combining below AND into one IN statement?

 IN[   (if(survey_no==1)  && employee_no== 'test')  ,  
       (if(survey_no==1)  && employee_no== 'test2') ,
        ...
   ]
like image 313
cometta Avatar asked Apr 27 '10 08:04

cometta


2 Answers

I think this is the criteria combination you want to use (btw. it is easier to help with the Hibernate entity bean definition instead of the table structure):

String[] employeeNames = { "test", "test2" };
List<Survey> surveys = getSession().createCriteria(Survey.class).add(
        Restrictions.and
        (
            Restrictions.eq("surveyNumber", 1),
            Restrictions.in("employeeName", employeeNames)
        )
    ).list();
like image 59
Daff Avatar answered Nov 20 '22 21:11

Daff


List<EmployeeSurvey> employeeSurvey = getSession().createCriteria
(EmployeeSurvey.class).add(
    Restrictions.and
    (
        Restrictions.eq("survey_no", 1), 
        Restrictions.in("employee_name", new String[] {"test", "test1"})
    )).list();
like image 2
Vinit Jordan Avatar answered Nov 20 '22 20:11

Vinit Jordan