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.  [ 1 post ] 
Author Message
 Post subject: ICriteria Query with many-to-many association
PostPosted: Thu Oct 07, 2010 7:14 pm 
Newbie

Joined: Thu Oct 07, 2010 6:40 pm
Posts: 1
Hello guys,

I have two classes Item and Category with a many-to-many relationsship (an item may be part of many categories and a category may have several items). Thus, I have an intermediate table ITEM_CATEGORY which consists of 2 columns, ITEM_ID and CATEGORY_ID.

When I execute following ICriteria query and examine the generated SQL-code with the NHibernateProfiler, it shows up that NHibernate uses two joins, one between the ITEM and ITEM_CATEGORY tables and the other between ITEM_CATEGORY and CATEGORY. However, only one join would be necessary to do this query, since I only need the categoryID for my restriction, which is also available in the intermediate table (no other fields of the Category class are used for the query).

Code:
Session.CreateCriteria<Item>()
    .CreateCriteria("Categories", JoinType.InnerJoin)
    .Add(Restrictions.IdEq(categoryId)))
    .List<Question>());


Is there a possibility to tell NHibernate to use the CATEGORY_ID column in the intermediate table and saving the unnecessary second join?

I know this query also may be done more easily by using HQL, but the above code should just serve as a simple toy example to reveal the problem.

Best regards,

chrisscha


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

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.