One of the things that has caused me headaches recently was the need to perform a certain task on each row in a large search result. Given the nature of the search and our Hibernate mappings, I needed to use an SQL query.
Unfortunately, I then found out that SQL queries don't support iterate! (I searched around and the only place that this appears to be documented is in the source code.)
So when all was said and done, I ended up falling back to straight JDBC, which is unfortunate, since now I have to maintain that code seperate from the mappings files if the schema changes.
Basically, all I am doing in JDBC is:
Code:
PreparedStatement statement = connection.prepareStatement(someSQL);
// statement.set ....
ResultSet rs = statement.executeQuery();
while (rs.next()) {
// Do the same thing to each result
}
It seems rather odd that I can't perform this type of simple task efficiently using Hibernate. (I could use the .list() method, but that would pull all the rows into memory before I could operate on them. I want to operate on them one at a time to minimize the amount of memory used by the task.)
I researched a bit more, and apparently even the iterate support for HQL queries would be somewhat inefficient for this type of task since it appears that it performs a select for each row in the result rather than just moving on to the next result. (Although it might be more efficient if your JDBC driver supports scrollable results).
That being said, what is the hibernate's team take on adding some form of "Closure" support to the Hibernate API?
Something like the Jakarta collections closure interface:
Code:
public interface Closure {
public void execute(Object input);
}
Then add a method to the query and Criteria interfaces to execute the Closure. Something like:
Code:
Closure closure = new Closure() {
public void execute(Object o) {
System.out.println(o);
}
};
Query hqlQuery = // create an HQL Query
hqlQuery.executeClosure(closure);
Query sqlQuery = // create an SQL Query
sqlQuery.executeClosure(closure);
Criteria criteria = // create a Criteria query
criteria.executeClosure(closure);
This would seem to be something fairly simple to implement. And using the XML anology, it would enable a SAX approach to solving a problem where appropriate, rather than the DOM approach almost mandated by the current Hibernate implementation.
Thoughts?