2010-04-27 54 views
5

我有表像下面hibernate restrictions.in與和,如何使用?

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 

如何通過下方爲一個結合IN語句Restriction.in查詢?

IN[ (if(survey_no==1) && employee_no== 'test') , 
     (if(survey_no==1) && employee_no== 'test2') , 
     ... 
    ] 

回答

5

我想這是你想要使用的標準組合(順便說一句,更容易幫助Hibernate的實體bean定義,而不是表結構。):

String[] employeeNames = { "test", "test2" }; 
List<Survey> surveys = getSession().createCriteria(Survey.class).add(
     Restrictions.and 
     (
      Restrictions.eq("surveyNumber", 1), 
      Restrictions.in("employeeName", employeeNames) 
     ) 
    ).list(); 
+3

限制被添加到條件默認爲AND子句。在這種情況下,代碼是正確的,但是如果兩個限制直接添加到條件而不是嵌套在限制條件和語句中,則會更簡單。 – Rachel 2010-04-27 12:34:41

+0

謝謝,很高興知道。似乎我後來提出了一些比他們實際需要更復雜的疑問。 – Daff 2010-04-27 16:27:27