2010-10-10 82 views
4

任何人都可以使用in 應用於列表中提示where子句的正確語法嗎?在一個.hbm文件以下查詢生成一個解析 異常:休眠查詢列表中的項目

<query name="Nutrient.findNutrients1"> 
    <![CDATA[from Nutrient as nutrient where nutrient.id in elements(?)]]> 
</query> 

例外如下:

PARSER.reportError(56)|行2:95: 期待IDENT,找到'?' SessionFactoryImpl。(395)|錯誤 在命名查詢中: Nutrient.findNutrients1 org.hibernate.hql.ast.QuerySyntaxException: 期待IDENT,找到'?' (?)近線 2,列95從營養的營養 其中nutrient.id在 元素

回答

4

刪除您的查詢的elements部分:

<query name="Nutrient.findNutrients1"> 
    <![CDATA[from Nutrient as nutrient where nutrient.id in (:ids)]]> 
</query> 

並調用它是這樣的:

List<Long> vals = Arrays.asList(1L, 2L); 

Query q = session.getNamedQuery("Nutrient.findNutrients1"); 
q.setParameterList("ids", vals); 
List<Nutrient> result = q.list(); 
+0

感謝您的支持。但是,我不確定如何在.hbm文件中定義的查詢中使用命名參數 - 我認爲您需要通過會話完成此操作...您知道是否可以通過.hbm完成此操作嗎? – Greg 2010-10-10 16:39:21

+0

@Greg您可以在命名查詢中使用位置參數或命名參數。但是,這不是重要的一部分。我可以刪除它,如果你喜歡:) – 2010-10-10 16:43:15

+0

@帕斯卡而不是刪除,你會介意添加一點點來展示如何傳遞在這個上下文中的命名參數? – Greg 2010-10-10 16:53:14