-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Closure support for hibernate?
PostPosted: Thu Apr 08, 2004 1:45 pm 
Beginner
Beginner

Joined: Fri Oct 10, 2003 4:54 pm
Posts: 26
Location: Chicago, IL
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?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.