Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate pagination or batch processing
    primarykey
    data
    text
    <p><strong>Question:</strong> How can I process (read in) batches of records 1000 at a time and ensure that only the current batch of 1000 records is in memory? Assume my primary key is called '<code>ID</code>' and my table is called <code>Customer</code>. </p> <p><strong>Background:</strong> This is not for user pagination, it is for compiling statistics about my table. I have limited memory available, therefore I want to read my records in batches of 1000 records at a time. I am only reading in records, they will not be modified. I have read that <code>StatelessSession</code> is good for this kind of thing and I've heard about people using <code>ScrollableResults</code>.</p> <p><strong>What I have tried:</strong> Currently I am working on a custom made solution where I implemented Iterable and basically did the pagination by using <code>setFirstResult</code> and <code>setMaxResults</code>. This seems to be very slow for me but it allows me to get 1000 records at a time. I would like to know how I can do this more efficiently, perhaps with something like <code>ScrollableResults</code>. I'm not yet sure why my current method is so slow; I'm ordering by ID but ID is the primary key so the table should already be indexed that way.</p> <p>As you might be able to tell, I keep reading bits and pieces about how to do this. If anyone can provide me a complete way to do this it would be greatly appreciated. I do know that you have to set <code>FORWARD_ONLY</code> on <code>ScrollableResults</code> and that calling <code>evict(entity)</code> will take an entity out of memory (unless you're doing second level caching, which I do not yet know how to check if I am or not). However I don't see any methods in the JavaDoc to read in say, 1000 records at a time. I want a balance between my lack of available memory and my slow network performance, so sending records over the network one at a time really isn't an option here. I am using Criteria API where possible. Thanks for any detailed replies.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload