-->
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: Finding all objects with
PostPosted: Tue Feb 10, 2004 11:58 am 
Newbie

Joined: Tue Feb 10, 2004 11:41 am
Posts: 4
Consider the classic Student -< Enrolment >- Course M-M relationship.

Im using <idbag> to hide the underlying associative table, so Student has a List of Courses.

Say i have a list of courses and i want to find all the students taking any of those courses.

Easy:
Code:
List  l = <list of course ids>
Query q = session.createQuery("select s.name from Student as s join s.courses as c where c.id in ( :cIds )");
q.setParameter("cIds", l);

But, what if i want to find the students who are taking all the papers?

I've been able to handle the case where my list is a know length in a rather ugly manner.

select distinct s.name from Students as s join s.courses as c1 join s.courses as c2 where c1 = :cid1 and c2=:cid2

q.setParameter("cid1", l.get(0));
q.setParameter("cid2", l.get(1));

Does anyone know how i can cope with this an a general, any number of courses manner?

I'm running MySQL 4.0 with InnoDB, in case it's relevant to the possible solutions (availablity of subselects etc).

TIA
dan.[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Tue Feb 10, 2004 9:14 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
You can use a count() in the select clause.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 5:50 am 
Newbie

Joined: Tue Feb 10, 2004 11:41 am
Posts: 4
Cheers, that's starting to make sense.

So i would get a result for each student who is taking any paper, then iterate through them and get the ones where count() = l.size().

Can i use it in the where clause too, so the query only gives me the results i want?


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.