I'm still a little new to hql and I have a question about the aggregation and performance functions for the query I'm writing.
Let's say this class is displayed in sleep mode (getters / seters / constructors / etc. Omitted for brevity):
public class Foo { private int i; private String s; private float f; }
I want to execute an hql query to get an instance of Foo with the highest value of i and the specified values ββof s and f. My current solution is this:
List<Foo> fooList = (List<Foo>)session.createQuery( "from Foo as foo where foo.s = :str and foo.f = :val order by foo.i desc"). setParameter("str", <stringgoeshere>).setParameter("val", <floatgoeshere>). list(); return fooList.get(0);
If I understand correctly, this method will N + 1 select the problem (although as long as I just grab the first result from the list, N will hopefully be 1, but still). I suppose there is a way to do this with uniqueResult (), but I donβt quite understand how aggregation functions will work in this case. The only examples I could find in "max ()" are either in the where clause or in the return value.
How do I write this query to do this in one select and grab the Foo instance with the highest i?
Thanks for the help!
source share