-->
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.  [ 4 posts ] 
Author Message
 Post subject: Conditional outer joins in HQL?
PostPosted: Mon Feb 23, 2004 12:30 pm 
Newbie

Joined: Tue Feb 10, 2004 11:41 am
Posts: 4
Are these possible in HQL?

I'll use the typical student - enrollment - course example to illustrate my situation.

Say i have table student which has a one to many relationship to enrollment which has a many to one relationship with course.

Using hibernates neat idbag feature, i hide the associative enrollment table so that each student has a List of courses.

I want all the students taking course 101 and 105 and 103 but not 106 or 107.

The list of required courses and excluded courses must be passed as paramters, and the size of the lists will vary every time.

After a bit of help on this board, i can do the first part of this query ( all the students taking 101 and 105 and 103) like so:

Code:
Integer[] ids = new Integer[]{new Integer( 101 ), new Integer( 103 ), new Integer( 105 )};

String queryString = "select s.id from Student as s join s.courses as c where c.id in (:idList)  group by s.id having count(s.id) >= :listSize";

query = session.createQuery( queryString );
query.setParameterList("idList",ids);
query.setParameter("listSize",new Integer(ids.length));


however, im having trouble specifing the second criteria, that i dont want students who have enrolled in a seperate list of course ids.


using SQL i can do with with a conditional outer join like so:

Code:
select s.id,s.name
from
  student as s
  join enrollment as e1 on s.id=e1.student_id
  left join enrollment as e2 on s.id = e2.student_id
    and e2.dimension_keyword_id in (106,107)
where
  e1.dimension_keyword_id in (101,103,105)
  and  e2.id is null
having count(e1.id) >=3;



Is there a way to do this in HQL? Hibernate doesn't seem to support conditions in it's join statement so im a bit lost. Perhaps some otherway to express the combination of must match, and must not match?

TIA,
dan.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 23, 2004 2:47 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
No, I'm sorry, it is not possible at present time. (It is a sensible future extension.)

Use a native SQL query.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 23, 2004 3:03 pm 
Newbie

Joined: Tue Feb 10, 2004 11:41 am
Posts: 4
Ok, a bit of off topicish help then...

Is there an equivalent of setParameterList() in JDBC?

Can't seem to find it anywhere... can't believe i will have to iterate over the course ids, and build up a hardcoded query string e.g...

Integer ids[] = ...

StringBuffer queryString " Select c.name from Courses as c where c.id in ("
for( int i =0 ; i< ids.length ; i++ )
{
queryString.append( ids[i].toString+", " )
}
queryString.append( ");");


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 25, 2004 10:31 am 
Newbie

Joined: Wed Feb 25, 2004 10:24 am
Posts: 2
Location: boston
I would really like to see conditional outer joins as well.

Rightnow, I am using createSQLQuery. However, this creates a new problem for me, since the APIs require a persister class as the return class. Thus it lost the flexibility if I could use "iterate". Since I need about 9 columns from 5 different tables.

I am just wondering that if people are going to use createSQLQuery which impies the sql is complex and there is not a natural mapping, would it be better to return Object[] as in "iterate" vs requiring a persister class.


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