-->
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.  [ 3 posts ] 
Author Message
 Post subject: Handling a large recordset
PostPosted: Mon Feb 23, 2009 11:07 am 
Newbie

Joined: Sat Feb 21, 2009 12:30 am
Posts: 7
I have a very large recordset that I need to read through sequentially and perform some process on each entry.

Rather than reading the ENTIRE recordset into memory how do I use Hibernate to read, let's say, one hundred records at a time? I know how to do this with a normal SQL call ( would create a query starts with the ID = 0 and reads one hundred records, saving the highest ID and then once I've processed that info, redo the query for the next 100 records > than the ID).

But How do I do that in Hibernate.... should I use HQL? or what?

Thanks for the help in advance


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 23, 2009 2:39 pm 
Expert
Expert

Joined: Fri Jan 30, 2009 1:47 am
Posts: 292
Location: Bangalore, India
Yes you can use the HQL. session.createQuery(HQLStr).setMaxResults(100).setFirstResult(0);
This will fetch the first 100 records.

_________________
Regards,
Litty Preeth


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 23, 2009 4:43 pm 
Newbie

Joined: Sat Feb 21, 2009 12:30 am
Posts: 7
Thank you very much.

I had been testing a possible solution and was about to post it and reply to my own post when I saw your answer. I modified my code to incorporate your stuff and it worked.

In my solution, I needed to move through all the records in the table (there are over 8000) about 100 at a time for memory purposes. I wound up having to save off the lastId of each group processed and issue another query starting from there. It's like paging through in a JSP.... something I had done tons of times before, but didn't know how to do it in Hibernate. Here's my solution:

Code:
    public void test(){
        Session session = HibernateUtil.currentSession();

        Query query = session.createQuery("from Stocks as stock where stock.id > :idValue");
        Stocks stock = null;
        List stocks = null;
        long lastId = 0;
        query.setLong("idValue", lastId).setMaxResults(20);
        stocks = query.list();
        while(stocks.size() != 0){
            for(int i = 0; i < stocks.size(); i++){
                stock = (Stocks)stocks.get(i);

               // DO ANY PROCESSING ON THE STOCK HERE


                log.debug((i + 1) + "Stock name = " + stock.getSymbol() + " id = " + stock.getId());
                lastId = stock.getId();
            }
            stocks = query.setLong("idValue", lastId).list();
        }


        HibernateUtil.closeSession();
    }


It's not very pretty or object oriented, but I'm in the process of creating a threadsafe Iterator class that gets each next record by paging through the database. It may be overkill, but I'm sort of a purist.

Stephen McConnell


"You are either part of the problem or part of the solution..... or maybe a precipitant or colloidal suspension..."
-- Lamar Stephens


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

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.