Reputation: 92099
I have been trying to fetch values from database using IN. I realized that I need to write JPA query like
o.country IN (’UK’, ’US’, ’France’)
so I tried to write query
List result = Playlist.find("id in ?", values).fetch();
where values=set of integers
but it fails to compile on runtime
java.lang.ClassCastException: java.util.HashSet cannot be cast to java.lang.Integer
How do I fix this?
Fixed
I also posted this on google groups and got the answer that seems to work
List<Integer> countries = (list of integers for ’UK’, ’US’, ’France’)
List result = Playlist.find("id in (:countries)").bind("countries",
countries).fetch();
Upvotes: 3
Views: 2031
Reputation: 157
I'm not alltogether sure about some aspects of your question:
Anyway here's an (modified and thus unchecked) example from my codebase:
In an JPA Entity class:
@NamedQuery(name = "findFoo", query = "select f from Foo f where f.state in :stateInList")
There query is used like so:
final Query query = this.entityManager.createNamedQuery("findFoo");
final Set<FooState> states = new HashSet<FooState>(Arrays.asList(FooState.STARTED, FooState.FAILED));
query.setParameter("stateInList", states);
final List<Foo> retval = query.getResultList();
return retval;
HTH
Upvotes: 2