I'm using JPA (Hibernate implementation). I've got a bean that has a List of beans and I want to load this list of beans.
Obviously I'd like not to load the whole list to memory at a time but to load the first elements, use them, free memory, load the next elements and so on. I know I can do this by using an offset in the SQL query, but I wonder if there's a better way to do this. A kind of semieager and semilazy load Any suggestions?
Thank you very much.
I feel there must be far better ways to do what I do... that makes learning even funnier
posted 8 years ago
Batch fetching seems to be close to what you desire.
See section 13.2.1 of Java Persistence with Hibernate. Pg. 574.
Using this you can process the elements in a list one by one, but can configure Hibernate to fire a single SELECT for 10 (batch-size) elements in one go.
You can clear your PersistenceContext after you process one batch before iterating to the next batch so that memory occupied by the previous batch is freed.