Hi, All,
To deal with a large result set(0.5~1M), I used this code fragment:
============================
Query query = getSession().createQuery("select p.lastName, p.firstName from Person as p where p.age= :age");
query.setLong("age", 21);
ScrollableResults res = query.scroll(ScrollMode.FORWARD_ONLY);
count = 0;
while (res.next())
{
String lName= res.getString(0);
String fName= res.getString(1);
//TODO biz logic starts here
count++
// clear cache to avoid memory issue
if ( count % 1000 == 0 ) {
getSession().flush();
getSession().clear();
}
}
res.close();
===============================
Two questions:
(1). Anything I can do here to make this loop fast? Current it is 3~4 times slower than that a single query to read all results into a list(consuming huge memory)
(2). Can I get Person object directly in ScrollableResults instead of these primitive types?
Platform: Hibernate 3.2, JDK-1.5, Oracle 10G, and XP/sp3 with 3G memory
Any help is greatly appreciated!
-Herbert
|