Quote:
Now it just so happens that I ran this query on months where no company had more than one approval, so I never noticed that the result set had each company listed once for each approval. So it turns out that I have the information I need; I can create a Map<Company, Integer> out of it and use that.
Now I might have misundestood your problem, but here is what I think.
If you run a query for companies joining in other stuff, the resulting list will still have as many elements as the underlying ResultSet, this results in the fact that you see a company multiple times, as they might have more than one joined row each.
So you can create a Set, and use that instead of the list, like this:
Before:
Code:
Query q = " ... ";
List l = q.list();
for (Object o: l) {
...
}
After:Code:
Query q = " ... ";
Collection c = new HashSet(q.list());
for (Object o: l) {
...
}
But again, that's pretty much what you suggested. I don't think you can have it any easier than that.
The group by does not work of course if you fetch company data. But you could skip loading company data (other than the id), if you had most of it in the 2nd level cache, in case it is enabled in your environment. Then you would just fetch companies by ids from the cache with session.load() while you were iterating the query results. It all depends on your application, and your caching strategy.
Roland