2016-04-22 75 views
0
String hql = "select * from myTable where isActive IN (:isActive)"; 
     Query query = session.createQuery(hql); 
     query.setString("school",""); 
     query.setString("isActive", "Y");//working 
     query.setString("isActive", "N");//working 
     query.setString("isActive", "Y","N"); // not working 
     query.setString("isActive", "'Y','N'"); // not working 
     return query.list(); 

我不知道如果下面的代碼應該工作,我想知道如果我可以傳遞值列表到我的搜索字符串參數,所以我不需要創建查詢;一個用於選擇所有數據而不管狀態,另一個用於僅選擇活動數據。查詢參數值的通過列表

回答

2

使用Query.setParameterList()List以作爲參數傳遞:

String hql = "select * from myTable where isActive IN (:isActive)"; 
Query query = session.createQuery(hql); 
List<String> isActiveList = new ArrayList<>(); 
isActiveList.add("Y"); 
isActiveList.add("N"); 
query.setParameterList("isActive", isActiveList); 
return query.list();