Working with part of a request at a time

I have a sleep request that returns 10s of thousands of object results. Is it possible to loop through pieces of a request rather than process the entire request at once?

Code example:

Query q = session.createQuery("FROM Class");


List<Class> classList = (List<Class>) q.list();

thank

+3
source share
2 answers

Use query.setMaxResults (int) and query.setFirstResult (int) to view the query.

Usually you should write a method that displays page x of the size of the result, where y is the number of elements on the page:

public List<Thingy> showThingies(int itemsPerPage, int pageOffest){
    // ..
    query.setMaxResults(itemsPerPage);
    query.setFirstResult(itemsPerPage * pageOffset);
    // ..
}
+1
source

You can use ScrollableResults from hibernate

Sort of:

ScrollableResults results = session.createCriteria(Project.class)
            .add(Restrictions.eq("projectType", Constants.REINDEX.PROJECT_TYPE))
            .setFetchSize(10)
            .scroll(ScrollMode.FORWARD_ONLY);

while (results.next()) {
        project = (Project) results.get(0);

, , , , .

10.4.1.6. , , , , , .

+1

Source: https://habr.com/ru/post/1772137/


All Articles