I am using Hibernate 4.1.6 and having issues with the speed that a list is built. I am running the following query.
public void doQuery(final Baz baz){
fi
I ran that query directly in the database and it returned 23,000 rows in 0.015 ms. So, I'm guessing the query is not the issue.
That may be premature, as query execution times depend on a lot more than the query text. Even if they queries are run on the same data, how do you know that the database used the same execution plan? How do you know that it gets the same amount of cache hits in its disk cache? For instance, hibernate uses prepared statements when talking to the database, but you probably didn't. In Oracle, execution plans are cached by query text, so a different query text means a freshly computed execution plan. Since the cached execution plan may have been formed based on different query parameters it may very well be different - and that can change execution times by orders of magnitude. Note that I am not saying that it is the database, but I wouldn't discount the possibility.
Therefore, the first thing you should do is measure whether the database, or something running in your JVM is wasting all that time. A simple way to do that is to watch the JVMs cpu consumption as the query is being executed. If it is significantly less than one thread, the JVM is waiting for something - presumably the database.
If it is the database, use the optimization tools of your database to capture the execution plan, and other relevant performance metrics.
If it is in the JVM, use a Profiler to pinpoint the performance bottleneck.