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.  [ 2 posts ] 
Author Message
 Post subject: Filtering collections of multiple objects with one query
PostPosted: Thu Apr 22, 2004 7:48 am 
Newbie

Joined: Thu Mar 04, 2004 5:03 am
Posts: 18
Assume the following class:

Code:
class A {
    Long id;     // PK
    Set dates; // Set of Date objects
}


Now say that I want to find all instances of class A that contain dates within a specified period, ie, given two dates, "from" and "to", find all instances of class A whose dates collection contains a date between "from" and "to".class A {

}

Given that my ddl looks like:

Code:
CREATE TABLE A (
  id int
);

CREATE TABLE A_dates (
  id_of_a int,
  date date
);


I would achieve the above requirement in SQL using the following pseudoquery which would list each instance of next to each date it contains:

Code:
SELECT A.id, A_dates.date from A LEFT JOIN A_dates ON A.id = A_date.id_of_a WHERE A.date >= "FROM" and A.date <= "TO";


How could I achieve the same in HQL?

I though of using a filter but it seems to me that you can only filter one particular collection each time so it wouldn't work for my case unless I filtered each instance of A separately which sounds like overkill.

Thanks for your help,
Giorgos


Top
 Profile  
 
 Post subject: got it
PostPosted: Thu Apr 22, 2004 11:45 am 
Newbie

Joined: Thu Mar 04, 2004 5:03 am
Posts: 18
I found the answer to my question and I am posting it here to help anyone with the same problem...

Basically what I was missing is that you can do joins in HQL agaists collections of values, and not only against other persistent entities.

So here is the HQL:
Code:
select A.id, date from A left join A.dates as date where date >= ? and date <= ?


Giorgos


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