I have a query where I get back millions of entities. I am doing calculations on each one and reducing the set down to thousands of entities. Now, I want to access a collection on each of those thousands of entities, but I am not sure how to performance optimize it.
I looked for something like session.fetch(entities, "dataPointBag"); but I couldn't find anything. If Hibernate has this functionality, please let me know because doing a method call like this seems to be the best solution.
The other ways I have thought about doing this: 1. Use all the IDs of the thousands of entities to query the collections directly, then programmatically fill the collections on the entities -- if I did this, wouldn't this interfere with Hibernate's dirty checks? I would not want to hibernate to write all the collections to the db. Could I possibly employ a trick where I set the thousands of entities to read-only, and then unset them from read-only? 2. Specify any fetch strategy other than lazy (eg, join, sub-select, batch) - this is a poor choice because it would needlessly get the collections for millions of entities instead of thousands 3. Collect all the IDs of the thousands of entities, and do query the db a second time for just those and use a fetch strategy other than lazy -- this is a poor choice because it would needlessly require getting all the entities from the db a second time
Is there a Hibernate api call to make this easy? Is getting the collections directly from the db and setting them myself the best option? Is there any other better way to do this?
Thanks, Bob
|