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;
}
}