开发者

Hibernate query where item in list

开发者 https://www.devze.com 2023-01-19 04:19 出处:网络
Can anyone suggest the correct syntax for a where clause using in 开发者_JAVA百科applied to a list? The following query in an .hbm file generates a parse

Can anyone suggest the correct syntax for a where clause using in 开发者_JAVA百科applied to a list? The following query in an .hbm file generates a parse exception:

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

The exception follows:

PARSER.reportError(56) | line 2:95: expecting IDENT, found '?' SessionFactoryImpl.(395) | Error in named query: Nutrient.findNutrients1 org.hibernate.hql.ast.QuerySyntaxException: expecting IDENT, found '?' near line 2, column 95 [ from Nutrient as nutrient where nutrient.id in elements(?)


Remove the elements part of your query:

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

And invoke it like this:

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

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

精彩评论

暂无评论...
验证码 换一张
取 消