Iterating results in Hibernate by R4R Team

If they are not already cached, iterate() will be slower than list() and might require many database hits for a simple query, usually 1 for the initial select which only returns identifiers, and n additional selects to initialize the actual instances.Occasionally, you might be able to achieve better performance by executing the query using the iterate() method. This will usually be the case if you expect that the actual entity instances returned by the query will already be in the session or second-level cache

// fetch ids
Iterator itr = session.createQuery("from r4r.Qux q order by q.likeliness").iterate();
while ( iter.hasNext() ) 
{
    Qux qx = (Qux) itr.next();  // fetch the object
    // something we couldnt express in the query
    if ( qux.calculateComplicatedAlgorithm() ) 
{
        // delete the current instance
        itr.remove();
        // dont need to process the rest
        break;
    }
}
Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!