2012-03-15 25 views
3

我想將整數列表傳遞給SQLQuery。然而,它拋出一個錯誤說"Exception : could not locate named parameter [ids]; nested exception is org.hibernate.QueryParameterException: could not locate named parameter [ids]"休眠與列表中的條件的SQLQuery

這是我的查詢看起來像:

List<Integer> ids = new ArrayList<Integer>(5); 
//Fill something in ids 
return session.createSQLQuery("select igf.foo_id from group_feed igf where igf.id in (:ids)") 
    .setMaxResults(pageSize) 
    .setParameterList("ids", ids) 
    .setResultTransformer(Transformers.aliasToBean(GroupFeed.class)) 
    .list(); 

我到底做錯了什麼?我不允許使用setParameterListHibernate SQLQuery?我無法從jBoss中的Hibernate文檔中取得很多成果。

回答

-1
List<Integer> ids = new ArrayList<Integer>(5); 

生成一個空的列表了500插槽容量:

ids {null, null, null, null, null} 

for (Integer i : ids) { 
      System.out.println("" + i); 
} 

會不會打印出任何東西。該列表是空的。

您確定列表中至少填有一項嗎?

的方式Hibernate查找參數我想你必須:

return session.createSQLQuery("select igf.foo_id from group_feed igf where igf.id in (:ids)") 
.setMaxResults(pageSize) 
.setParameterList("ids", ids) 
.setResultTransformer(Transformers.aliasToBean(GroupFeed.class)) 
.list(); 

我覺得Hibernate是不知道:ids的,因爲它是(:ids)

+0

仍然一樣。例外說:「例外:無法找到命名參數[ids];」 。我想這是指沒有找到'ids'數據的事實。 – 2012-03-15 13:53:13

+0

我不想把初始化代碼放在這裏,所以我把這個註釋 - > //填充到id中。我想@Vikram說的是正確的。 – 2012-03-15 14:26:05

+0

我明白你不想:) – ssedano 2012-03-15 14:29:22