-->
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: Many-to-many collections retrieval & filtering logic
PostPosted: Tue Jun 19, 2007 2:09 pm 
Newbie

Joined: Tue Jun 19, 2007 1:16 pm
Posts: 2
Hibernate version: 2

Hi, I am a newbie in hibernate and definitely confused.

I have a classic many-to-many relation (e.g. Category and Item). The relational table doesn't have it's own id but uses a combined key consisting of the 2 foreign keys (e.g <CAT_ID, ITEM_ID>). Both collections (Category.items, Item.Categories) are lazy loaded.

I want to retrieve the Item.Categories ids without loading the categories collection.

My first approach was using a query like
Code:
select cat.id from Category cat where :item in elements(cat.items)


My second approach trying to avoid inner selections was
Code:
select cats.id from Item item join item.categories cats  where item = :item


Both of them worked fine, but then I was urged to use filtering, so here are my questions :

1) Is there any point trying to use filtering in this case if the collection may not be initialized or referenced (net.sf.hibernate.LazyInitializationException)?
2) If yes how can I do this? What filter should I use (e.g. "select this.id").
3) What is the practical difference with the previous approaches?

Thanks in advance for your time.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 19, 2007 6:45 pm 
Regular
Regular

Joined: Wed May 05, 2004 8:01 am
Posts: 53
First of all if you have declared many to many relationship in both direction you can simply do:

item = session.load( Item.class, id );

Set<Category> categories = item.getCategories();

that is all.

If for some reason there is not item.categories and only category.items than you may use this query:

select c.id from Category c join c.items i where i = :chosenItem

Please rate if that helped.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 20, 2007 8:10 am 
Newbie

Joined: Tue Jun 19, 2007 1:16 pm
Posts: 2
Thanks! It seems to be working now. Actually I missed the loading part. When I loaded the item object and then applied a filter "select this.id" on the categories collection, it worked fine.

On the other hand, in terms of optimization, since joining is not avoided, shouldn't be better to choose the 2nd approach (HQL join query), in order to avoid the loading overhead?


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.