-->
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: List Members in Where Clause
PostPosted: Fri Apr 10, 2009 10:27 pm 
Newbie

Joined: Fri Apr 10, 2009 8:57 pm
Posts: 8
Suppose I have the following classes:

class Foo {
@OneToMany
List<Bar> bars;
}
class Bar {
String s;
}

And I want to write a method like this:

List<Foo> getFoosContainingAny(List<String> keywords)

The purpose here is to retrieve any Foo f such that a Bar b in its list satisfies:

keywords.contains(b.s)

I'm not sure how to express this in HQL, but I suspect it will require some clever combination of an outer join and the "in elements" operator. Any gurus want to take a stab at this?

BTW, this would make a truly evil interview question. Feel free to use, but not on me.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Apr 12, 2009 11:17 pm 
Newbie

Joined: Sat Mar 28, 2009 5:47 pm
Posts: 14
without thinking too deeply, or even trying to test ;)
something like this:


Code:
session.createQuery("select distinct f from Foo as f join f.bars as b where b.s  in (:keys)")
    .setParameterList("keys", keywords).list();


should return a List<Foo> as you want.
see: http://forum.springsource.org/showthread.php?t=38790


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 14, 2009 3:39 am 
Newbie

Joined: Fri Apr 10, 2009 8:57 pm
Posts: 8
Thanks for the response, it makes sense to me, I just didn't know something like "f join f.bars" would work in HQL. I'll give it a try this weekend, and if it works I'll come back and credit your answer.


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.