Hibernate查询列表中的项目

时间:2010-10-10 02:19:16

标签: hibernate hql

任何人都可以使用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 as   养分。营养素   元件(?)

1 个答案:

答案 0 :(得分: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();