2012-03-01 38 views
3

如何在Play中執行查詢!帶IN條件的框架?我已經按照此鏈接Play Framework - How to Query a Model using 'IN'?的解決方案,但我得到一個非法Arugment異常錯誤:使用IN條件播放框架查詢

IllegalArgumentException occured : org.hibernate.hql.ast.QuerySyntaxException: cannot define positional parameter after any named parameters have been defined [from models.Post where (category_id in (:cat)) AND (gender like ?) AND (name like ? OR description like ?) AND isComplete is ? order by created_date desc] 

,這是我的代碼..

 String arrString[] = category.split(","); 
     List<String> listString = new ArrayList<String>(); 
     for(int i=0; i<arrString.length; i++) { 
      listString.add(arrString[i]); 
     } 

     posts = Post.find(     
      "(category_id in (: cat)) AND (gender like?) AND (name like? OR description like?) AND isComplete is? " + 
      "order by created_date desc", gender, search, search, true 
     ).bind("cat", listString).from(0).fetch(50); 

我不能簡單地使用(? )IN語法。所以它會是這樣的:

 posts = Post.find(     
      "(category_id in (?)) AND (gender like?) AND (name like? OR description like?) AND isComplete is? " + 
      "order by created_date desc", category, gender, search, search, true 
     ).from(0).fetch(50); 

我在哪裏做錯了?任何意見,將不勝感激。

回答

1

的東西,如

import play.db.helper.SqlQuery; 
... 
Set<String> categories = ...; 
posts = Post.find("(category_id in "+SqlQuery.inlineParam(categories)+" AND ...")....fetch(); 
0

您的代碼應該工作,請嘗試以下操作:

posts = Post.find("category_id in (?)", ids).from(0).fetch(50); 

下面的代碼工作對我的EntityManager:

JPA.em().createNativeQuery("select from posts where category_id in (:ids)").setParameter("ids", ids).getResultList(); 

其中 「ids」 是「 List<Integer> ids