-->
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: One-to-Many based on Parameter
PostPosted: Sat Dec 29, 2007 2:16 pm 
Newbie

Joined: Sat Dec 29, 2007 12:31 pm
Posts: 2
Hibernate version:3.1.3

I have a one-to-many association between GROUP and QUESTION

This works fine when I need to get all QUESTION entries for a GROUP. The resulting query when a left join is used (simplified):
select
g.*, q.*
from
GROUP g
left outer join QUESTION q
on g.ID = q.GROUP_ID

I want to be able to add a filter / parameter that would result in the following query:
select
g.*, q.*
from
GROUP g
left outer join QUESTION q
on g.ID = q.GROUP_ID and q.ACTIVE = 'Y'

I have tried using a filter but that only adds to the where clause. That does not give me the results I need because I still want the group to be returned if it has not active questions.

I am trying not to use HQL in the case, I want to use XML mapping if at all possible.

Is it possible to add criteria to the "on" clause of a join at runtime?


Top
 Profile  
 
 Post subject: Re: One-to-Many based on Parameter
PostPosted: Sat Dec 29, 2007 3:22 pm 
Expert
Expert

Joined: Wed Apr 11, 2007 11:39 am
Posts: 735
Location: Montreal, QC
I am a little bit confused by your question when you say you favour an XML mapping over a HQL command. If you need this filter over your collection you may want to set a where attribute for one-to-many association which will be added to its outer join criteria. However, you might also want to use HQL to only do this filtering at runtime and for specific use cases. Then you might want to do something like:

select g from Group g left outer join g.questions q with q.active = 'Y'


Please give more details of what you want to do if neither cases are suitable for you.


Farzad-


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 29, 2007 8:10 pm 
Regular
Regular

Joined: Sat Nov 25, 2006 11:37 am
Posts: 72
Try:

select
g.*, q.*
from
GROUP g
left outer join QUESTION q
on g.ID = q.GROUP_ID and (q.ACTIVE is null or q.ACTIVE = 'Y')


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 29, 2007 8:11 pm 
Regular
Regular

Joined: Sat Nov 25, 2006 11:37 am
Posts: 72
Sorry I meant:


select
g.*, q.*
from
GROUP g
left outer join QUESTION q
on g.ID = q.GROUP_ID
where q.ACTIVE is null or q.ACTIVE = 'Y'


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 31, 2007 10:02 am 
Newbie

Joined: Sat Dec 29, 2007 12:31 pm
Posts: 2
malm66 adding "q.ACTIVE is null or q.ACTIVE = 'Y'" to the where clause would not give the same results as a left join.

e.g. A GROUP that has only one question where q.ACTIVE = 'N' would return no results when the condition you suggest is added to the where clause.

select
g.*, q.*
from
GROUP g
left outer join QUESTION q
on g.ID = q.GROUP_ID and q.ACTIVE = 'Y'

Will return one GROUP entry with the QUESTION portion of that being null.


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.