These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: How can I programmatically populate collections on an entity
PostPosted: Thu Apr 22, 2010 12:01 pm 
Newbie

Joined: Thu Apr 22, 2010 11:21 am
Posts: 3
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


Top
 Profile  
 
 Post subject: Re: How can I programmatically populate collections on an entity
PostPosted: Fri Apr 23, 2010 5:07 pm 
Newbie

Joined: Fri Sep 29, 2006 6:35 pm
Posts: 6
Employing alternative 1, if you've closed your session after the first fetch, the entities you fetched are detached, so will not be written back to the database unless you take measures to reattach them (e.g., update()). That means you could replace their collections with ones fetched from the db, then pass them around the rest of your application code as complete objects (i.e., fully populated).

Are you planning on saving the thousands of entities when you're done? If so, you may, instead, need to maintain the retrieved collections in a map (keyed by entity id) and leave the fetched entities' collections alone. Not ideal, admittedly. Maybe someone else has a better idea.


Top
 Profile  
 
 Post subject: Re: How can I programmatically populate collections on an entity
PostPosted: Sun Apr 25, 2010 11:12 am 
Newbie

Joined: Thu Apr 22, 2010 11:21 am
Posts: 3
Detaching, populating and reattaching should work, but maybe making them read-only, populating and then back to not read-only would be a bit faster? But I will want to persist updates in the entities in the collections, so I am nervous about

Yes, I will need to persist the changes to the thousands of entities, and the entities in their collections. One of the issues is that it may be hard to guarantee that none of the code will look at the collection from the main entity list. So even if I write code to put the collections in a map and look them up by the main entity id, I would still potentially have a big performance hit.

I am not sure why Hibernate wouldn't give developers a way to populate collections for a list of entities, so I am really hoping there is a way.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.