2010-10-10 78 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