• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Loading part of a List with JPA

 
Antonio Fornie
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello.

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.
 
Onkar Joshi
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic