sarahsmith_books wrote:
Quote:
Code:
Integer myCount (Integer) session.iterate("select count(id) from tab where statues = 2").next()
Hi Preston,
I just used this in place of a regular query because I didn't want to go through retrieving the result set and then getting the size of it to find out the count - and I have a couple of questions about it.
First, I noticed that the current Session class doesn't have iterate methods so I had to go back to classic hibernate. Is this okay? I was under the impression that classic hibernate was old and we shouldn't be using it.
Second, is there some way to use a named query for the string we pass to the interate method? I'm trying to keep my source code free of sql.
Thanks!
I'm sure there are many ways you can do this. Just give it a try.
If you had read the comments in the deprecated iterate() method, you'd see that they suggest using find(). And I can't see why there would be any problems using a Named Query. It will still return a List with the first element being and Integer. WRITE SOME CODE, RUN IT, SEE WHAT HAPPENS.