-->
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.  [ 6 posts ] 
Author Message
 Post subject: Join 3 Tables (A, B, C) where B and C reference A
PostPosted: Tue Jul 15, 2008 8:49 am 
Newbie

Joined: Tue Jul 15, 2008 8:27 am
Posts: 12
Location: Germany
Hibernate version: 1.2

Name and version of the database you are using: MS Access

Hi all,

I want to use HQL to join 3 Tables, A, B and C, where B and C have (n : 1) Relations to A

SQL would look something like:

SELECT * from (A LEFT JOIN B ON A.ID = B.ID) LEFT JOIN C ON A.ID = C.ID
WHERE ...

In HQL I tried the following

a)

SELECT b.A from B AS b LEFT JOIN A AS a, C AS c
WHERE b.A = c.A ...

b)

SELECT a from A AS a, B AS b, C AS c
WHERE a = b.A AND a = c.A ...

Both a) and b) queries do not work... Any ideas?

Thanks a lot,
Cheers, Arndt


Top
 Profile  
 
 Post subject: Re: Join 3 Tables (A, B, C) where B and C reference A
PostPosted: Tue Jul 15, 2008 3:57 pm 
Newbie

Joined: Sun Mar 30, 2008 5:19 pm
Posts: 8
when you say the queries don't work, what error message are you getting?

also post your mapping files so I can see the fields you're using to join the tables with.


Top
 Profile  
 
 Post subject: Re: Join 3 Tables (A, B, C) where B and C reference A
PostPosted: Tue Jul 15, 2008 4:41 pm 
Newbie

Joined: Tue Jul 15, 2008 8:27 am
Posts: 12
Location: Germany
dannyhoult wrote:
when you say the queries don't work, what error message are you getting?


The message is either: Cannot Execute SQL
or: unexpected token: B
or: missing path statement

dannyhoult wrote:
also post your mapping files so I can see the fields you're using to join the tables with.


Class A has no reference to B or C

A join either B on A or C on A works, it is just the problem when I want to join all 3 tables, so:

A: no ref to either B or C
B: <many-to-one class="A".../>
C: <many-to-one class="A".../>

There should nothing be wrong with the mapping files since they work:

in the DB it is like:

B has foreign key to A.Id
C has foreign key to A.Id
A has no ref to either

Hope that clarifies...

Actually I thing I will just use SQLQuery for it does not seem that HQL can handle this case - no such case in the docs anyway...

Cheers, Arndt


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 15, 2008 4:58 pm 
Newbie

Joined: Sun Mar 30, 2008 5:19 pm
Posts: 8
Quote:
A: no ref to either B or C
B: <many-to-one class="A".../>
C: <many-to-one class="A".../>


you're looking for all A's that are attached to both B's and C's correct?

unless you have links from A to B and A to C or a link between B and C you can't do the join in SQL without using something like a union, which you can't do using HQL.

I would suggest either using SQL, or adding the links in A to both B and C.
Then you could do something like:
select a from A as a left join a.B as b left join a.C as c where b.ID is not null and c.ID is not null

?


Top
 Profile  
 
 Post subject: Seems so...
PostPosted: Tue Jul 15, 2008 5:41 pm 
Newbie

Joined: Tue Jul 15, 2008 8:27 am
Posts: 12
Location: Germany
Thank you for reconfirming this...

As I said, I am already looking into the SQLQuery.

Again thanks and cheers,
Arndt


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 17, 2008 3:32 pm 
Newbie

Joined: Sun May 18, 2008 1:21 pm
Posts: 13
I agree with dannyhoult quote

because

Remember, HQL is not SQL and works on Entities, not tables. You must
use property names, not column names, and you must join on objects
paths, not on relations.

_________________
Zafar Ullah

Lead/Architect
www.lmkr.com
www.apextechnologies.ca
Blog
http://barchitect.blogspot.com


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