-->
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.  [ 5 posts ] 
Author Message
 Post subject: Query based on contents of collection
PostPosted: Tue Nov 07, 2006 9:23 am 
Newbie

Joined: Sun Oct 29, 2006 5:44 pm
Posts: 10
Location: New Zealand
I'm not 100% sure how to ask this question so searching the forums and goolge haven't been very helpful. Here is my situation:

I have a many-to-many relationship between Parts and Categories (a part may belong to many categories and a category may describe many parts). This is fine, and all works well.

What I am wanting to do is filter the parts based on whether they belong to all categories of a selected list (ie the user selects multiple categories and the program finds the parts that fit into every category).

I have been trying along the lines of:
Code:
from Part as part where (? in (select elements(AircraftModels) from part) and ? in (select elements(AircraftModels) from part) and......)

with each category in the list. Not only does it not work, it is incredibly ugly and I don't want to use it!! There must be a better way, however I cannot see anything in the manual that jumps out as being on the right track.

Any suggestions?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 07, 2006 3:43 pm 
Beginner
Beginner

Joined: Fri Oct 20, 2006 8:02 am
Posts: 36
If I understand correct, it should work:

Code:
select elements(C.Parts) from Category C where C.Id in (<previous selected ids>)
[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 07, 2006 5:40 pm 
Newbie

Joined: Sun Oct 29, 2006 5:44 pm
Posts: 10
Location: New Zealand
Edit:Duplicate post sorry


Last edited by moebius on Tue Nov 07, 2006 6:33 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 07, 2006 5:41 pm 
Newbie

Joined: Sun Oct 29, 2006 5:44 pm
Posts: 10
Location: New Zealand
anuarneto: Thanks - not quite what I was after, but I think I'm nearing the right track.

This is what I have now:
Code:
"select distinct part from Part as part where part.PartCategories = any indices(:categories)"


It's throwing a QueryException: "unindexed collection before []" though.

I think it might be something to do with the way the collections are set up - I'm using IList<> in code, and bag in .hbm.xml.
I just read in the manual that the bag is unindexed and unordered etc, so I've tried to change it to other things, like set and list. All of these throw errors.

Which pair of things (collection in code and collection in .hbm.xml) should I use that are indexed AND handle many-to-many?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 11, 2006 4:53 am 
Senior
Senior

Joined: Mon Aug 21, 2006 9:18 am
Posts: 179
Hi,
You might want to try a <list/> mapping collection that maps to an IList<T>.

Here's a snippet of how I use it for an address object:
Code:
<list name="StreetFields" cascade="all-delete-orphan" table="StreetField" >     
      <key column="StreetAddressId" foreign-key="StreetAddress_StreetField"/>
      <index column="Position" />     
      <one-to-many class="Cei.eMerge.Core.Domain.Contacts.StreetField, Cei.eMerge.Core" />
    </list>


Within HQL I can reference fields according to their index ('Position') .

If you are using the 'indices' function in HQL, you need to have use the list.

Hope this helps
MIKE

_________________
If this helped...please remember to rate it!


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