Hibernate request where an item is in a list

Can someone suggest the correct syntax for the where clause using in applies to the list? The following query in the .hbm file throws an exception parsing:

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

An exception:

PARSER.reportError (56) | line 2:95: waiting for IDENT, found '?' SessionFactoryImpl. (395) | error in named query: Nutrient.findNutrients1 org.hibernate.hql.ast.QuerySyntaxException: while waiting for IDENT, found '?' close line 2, column 95 [from the nutrient as nutrients, where nutrient.id in the elements (?)

+4
source share
1 answer

Remove the elements part of your query:

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

And call 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(); 
+4
source

All Articles