java – HQL query with ENUM list as parameter

Question:

Hello, I have the following problem, here is an example:

I have a Banda entity that has as an attribute a list of List<Generos> generos , Generos is an ENUM with the following values: ALTERNATIVE_ROCK("Alternative Rock"), CLASSIC_ROCK("Classic Rock"), HARD_ROCK("Hard Rock"), HEAVY_METAL("Heavy Metal"),PROGRESSIVE_ROCK("Progressive Rock");

I'm trying to create a method that returns a List<Banda> passing as parameter a list of ENUM List<Generos> using HQL… type public List<Banda> retornaBandasPorGenero(List<Generos> generos); but without success, what is the best way to solve this?

Answer:

Use the setParameterList method when binding the respective parameter.

Example:

Generos[] paramGeneros = {Generos.ALTERNATIVE_ROCK, Generos.CLASSIC_ROCK};
IQuery minhaQuery = sessao.createQuery("from Banda b where b.Genero in (:colecaoGeneros)");
minhaQuery.setParameterList("colecaoGeneros", paramGeneros); // Olha ele aí!
Scroll to Top