Play! Framework - Running JPA query for range of values using IN
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();
I'm not alltogether sure about some aspects of your question:
- I don't know anything about the Playlist class and it's methods.
- What do you mean by "but it fails to compile on runtime"? Does it fail to compile or does it fail to run?
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
精彩评论