I have built a NamedQuery that looks like this:
@NamedQuery(name = "EventLog.viewDatesInclude",
query = "SELECT el FROM EventLog el WHERE el.timeMark >= :dateFrom AND "
+ "el.timeMark <= :dateTo AND "
+ "el.name IN (:inclList)")
What I want to do is fill in the parameter :inclList with a list of items instead of one item. For example if I have a new List<String>() { "a", "b", "c" }
how do I get that in the :inclList parameter? It only lets me codify one string. For example:
setParameter("inclList", "a") // works
setParameter("inclList", "a, b") // does not work
setParameter("inclList", "'a', 'b'") // does not work
setParameter("inclList", list) // throws an exception
I know I could just build a string and build the whole Query from that, but I wanted to avoid the overhead. Is there a better way of doing this?
Related question: if the List is very large, is there any good way of building query like that?
When using
IN
with a collection-valued parameter you don't need(...)
:Works for me with JPA 2, Jboss 7.0.2
You must convert to
List
as shown below:The proper JPA query format would be:
If you're using an older version of Hibernate as your provider you have to write:
but that is a bug (HHH-5126) (EDIT: which has been resolved by now).