-->
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: inner select in HQL possible?
PostPosted: Thu Feb 08, 2007 7:19 pm 
Newbie

Joined: Thu Feb 08, 2007 6:40 pm
Posts: 2
I'm attempting to write an HQL statement that uses an inner select, but Hibernate's complaining about invalid syntax. I'm fairly certain I've got something wrong, but I can't find information about how to do an inner
select with HQL.

I'm attempting to select object a in two buckets - those with a b.name of 'FOO', and those with either no b record, or with b.name as anything else.
I'm interested in all a objects, and the b.name values of those that meet
the criteria.


I'd like to write something like so

Code:
from a
  left outer join (select b.id, b.name, b.a_id
                   from b
                   where name = 'FOO') as b2



Hibernate says that the ( in 'left outer join (select' is incorrect.

Is this in the reference guide, or elsewhere? My google-fu is lacking today.

Thanks for your help.
- Mark


Top
 Profile  
 
 Post subject:
PostPosted: Thu Feb 08, 2007 8:24 pm 
Newbie

Joined: Thu Feb 08, 2007 7:53 pm
Posts: 5
When writing HQL, you need to think in terms of object relationships. In the content you've provided there is no stated relationship between there 'a' and 'b' classes. If you can clarify their relationship here, we'll better understand your issue.

An idea for your own effort is to work out the actual SQL you want to generate, and work backwards, using your Hibernate mapping files, to arrive at the correct HQL.

dlgrasse


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 09, 2007 12:35 am 
Expert
Expert

Joined: Tue Jan 30, 2007 12:45 am
Posts: 283
Location: India
Hi dlgrasse,

try to add child criteria in parent criteria.will give you inner join.But it would return all the child in parent object if any child match with criteria otherwise will return null

_________________
Dharmendra Pandey


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 12, 2007 9:27 am 
Newbie

Joined: Thu Feb 08, 2007 6:40 pm
Posts: 2
dharmendra.pandey's got it right, but I didn't make a very clear example.

Let's say we've got three tables.

A
id
value

B
id
a_id
c_id

C
id
lookup_value (values WANTED, UNWANTED)

Assume A's value is what we really want. C is a lookup table, and B joins the
two in a many-many, represented as a collection between the java versions of A and C in the mapping files.

Let's also say that C has several values for 'lookup_value'. I'm interested in
finding only A's value for lookup_value of 'WANTED' - every other lookup value in C should be considered 'NULL'. So, what I really want is a C' table
that only has 2 values for 'lookup_value', WANTED or NULL.

If I put that restriction down in the SQL where clause eg.

select a.id, a.value
from a
,join b on a.id = b.a_id
, left outer join c on b.c_id = c.id
where c.lookup_value in (WANTED, NULL)

I get only the a records that have a c.lookup_value of WANTED or NULL.
If an a record has only a lookup_value of UNWANTED, the record goes missing.

If instead we were to manually create a temporary table of C that had
only the WANTED lookup records in it, we could join it with an outer join
and effectively create two buckets - WANTED and null.

insert into c_wanted
(select c.* where c.lookup_value = 'WANTED');

select a.id, a.value
from a
,join b on a.id = b.a_id
, left outer join c_wanted on b.c_id = c.id;

(not actually attempted)

What I'd like to do is stick the selection on c as an inner-join clause, so the result of that inner query is treated like a temporary table, before getting processed in the where clause.

select a.id, a.value
from a
,join b on a.id = b.a_id
, left outer join (select c.* where c.lookup_value = 'WANTED');

Something along those lines.

Thanks again for everybody's help.


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.