Retrieving Hibernate query result as result set instead of list

前端 未结 3 1341
耶瑟儿~
耶瑟儿~ 2021-01-12 22:41

Heya, I m new to hibernate. I have to say it really simplifies everything for the SQL query. However, manipulating the returned result is a headache for me at the moment.

3条回答
  •  粉色の甜心
    2021-01-12 23:21

    You may need this if you have huge database and you can't fit List result into memory. Use scroll() instead of list():

    Query query = session.createQuery(query);
    query.setReadOnly(true);
    setFetchSize(Integer.MIN_VALUE); //MUST use Integer.MIN_VALUE, other value=fetch all
    ScrollableResults results = query.scroll(ScrollMode.FORWARD_ONLY);
    // iterate over results
    while (results.next()) {
        Object row = results.get();
    }
    results.close();
    

提交回复
热议问题