G__
G__

Reputation: 7111

Hibernate query where item in list

Can anyone suggest the correct syntax for a where clause using in 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(?)

Upvotes: 4

Views: 12725

Answers (1)

Pascal Thivent
Pascal Thivent

Reputation: 570295

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();

Upvotes: 4

Related Questions