Hello,
I have following problem that I couldn't solve by google or testing yet:
I have to process all entities from a relationship. The number of entities is around 15.000 and I have to go through all of them. The used memory after loading them rises by about 100MB and it is foreseeable that this is a risk of an OutOfMemory exception, if the number of entities rises further. I'm now looking for a way to iterate over the entities without produciong too much load on memory or the database.
the first idea was to use Query.iterate(), but the API description for Query.iterate() says
Quote:
Entities returned as results are initialized on demand. The first SQL query returns identifiers only.
This sounds like each time In call next() on the iterator it would execute a query (SELECT ... FROM ... WHERE ID = ...). I hope that I'm wrong.
A colleague suggested to use fetchSize and cacheMode to improve that, but fetchSize doesn't help if Query.list is actually returning the complete list of objects as ArrayList. And I haven't found any useful documentation about the CacheModeType enum.
So what is the best practice way to get a stream of entities back from Hibernate/JPA without hammering the database and skyrocketing the memory?
Cheers,
SF