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.  [ 6 posts ] 
Author Message
 Post subject: Need help with query
PostPosted: Thu Jan 29, 2009 4:28 pm 
Newbie

Joined: Tue Sep 18, 2007 10:35 am
Posts: 8
I have a many to many relationship between Articles and Categories which is expressed as a bi directional relationship in code. I'd like to query for all articles except the ones that are related to certain categories. My first inclination is to loop through the excluded categories and build the HQL query string like "category.Id not in (1, 3, 6, ..)" but I'd prefer to use a parametrized query if possible. I'm having trouble figuring out how to express this in HQL or query by criteria. Anyone know how to do this?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 02, 2009 6:03 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
I think this should work:

Code:
IQuery query = session.CreateQuery("from Articles a join fetch a.Categories c where c in (:CategoryList).SetParameter(categoryList);

categoryList is a List<Category> or Category[] or something else that is IEnumerabe. If you only have the ids available you can change the query to "c.id in (:categoryIdList)" and pass a list of ids.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 02, 2009 1:52 pm 
Newbie

Joined: Tue Sep 18, 2007 10:35 am
Posts: 8
Couple of things. I want to find all the articles that are NOT related to certain categories so maybe I need to use "not in" instead of "in"?

Also, I get an error when I run this query saying that NHibernate cannot resolve the property Categories on the object Article even though there is one. Categories is a read only IEnumerable<Category> backed by a field saved by field access. Does there need to be a setter for this to work?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 02, 2009 2:13 pm 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
If you have field access, you have to use the field name, not the property name. Probably:

Code:
IQuery query = session.CreateQuery("from Articles a join fetch a.categories c where c in (:CategoryList).SetParameter(categoryList);


And you need "not in", of course. Sorry.

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 02, 2009 4:18 pm 
Newbie

Joined: Tue Sep 18, 2007 10:35 am
Posts: 8
Thank you so much. One more question.

How would I modify the query so that only distinct results are returned? As is, I get a lot of duplicates.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 02, 2009 4:37 pm 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You can try

"select distinct a from Articles a ..."

or

query.SetResultTransformer( new DistinctRootEntityResultTransformer() )

You're welcome.

_________________
--Wolfgang


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.