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: Path expression, HQL, Join - How to solve this?
PostPosted: Thu Mar 06, 2008 9:15 am 
Newbie

Joined: Thu Mar 06, 2008 8:27 am
Posts: 7
Hibernate version: 1.2

Full stack trace of any exception that occurs:outer or full join must be followed by path expression [...query (see below)...]

Name and version of the database you are using: MSSQL Server 2005

The generated SQL (show_sql=true): none


Hi there!

I am using NHibernate together with Spring.NET, using the HibernateTemplate, Spring.NET supplies me with.

Everything always worked fine for me, besides the typical problems one encounters when using these two for the first time...

Well, now I have come to the point where I just can't help me myself and have to ask.

I am trying to do quite a huge query including joins over 7 tables...

The problem is, that I have this linear structure of seven tables and want to get some data from table number 7 depending on a value found in table number 1.

As I am not allowed to post table names and stuff here, I will name them table_1 to table_7 to obfuscate names, hope that's ok!?

Alright, first the tables:

table_1 has a value I get from somewhere (an id).
table_2 has a FK pointing to this id, as well as a FK pointing to table_3.
In table_3 there's nothing interesting by now.
table_4 contains a FK pointing to table_3, as well as a FK pointing to table_5.
table_6 has a FK pointing to table_5, as well as a FK pointing to table_7 where I want to get the data from.

In the query below, something like "table_6.five_id.id" means: In Table 6, there is a foreign key pointing to table 5.

In objects, this would be something like: A person has a phone number which has an id (where person would be table_6, five_id the FK to phone_number and id the number's id).



So this is my HQL (hope so) query:

Code:
from Table_1 table_1, Table_7 table_7, Table_5 table_5
inner join Table_6 as table_6 on table_5.Id = table_6.five_id.Id
inner join Table_4 as table_4 on table_5.Id = table_4.five_id.Id
inner join table_7 on table_6.seven_id.Id = table_7.id
inner join Table_3 as table_3 on table_4.three_id.Id = table_3.Id
inner join Table_2 as table_2 on table_3.Id = table_2.three_id.Id
inner join table_1 on table_2.one_id.Id = table_1.Id
where table_1.Id = :OneIDWhichIsPassedByMe


which I derived from MSSQL Server's generated query:

Code:
SELECT   
dbo.table_1.interesting_value, dbo.table_7.interesting_value

FROM
dbo.table_5

INNER JOIN
dbo.table_6 ON dbo.table_5.id = dbo.table_6.five_id

INNER JOIN
dbo.table_4 ON dbo.table_5.id = dbo.table_4.five_id

INNER JOIN
dbo.table_7 ON dbo.table_6.seven_id = dbo.table_7.id

INNER JOIN
dbo.table_3 ON dbo.table_4.three_id = dbo.table_3.id

INNER JOIN
dbo.table_2 ON dbo.table_3.id = dbo.table_2.three_id

INNER JOIN
dbo.table_1 ON dbo.table_2.one_id = dbo.table_1.id

WHERE
dbo.table_1.id = @myRequestedId


This actually is the first time I am working with HQL and JOINs, so please be patient if the above stuff is simply stupid...or something...

The problem: I am getting the exception I mentioned above. I do not get any generated SQL.

I tried to get all the tables manually (from NHibernate/Spring --> HibernateTemplate.LoadAll<MyType>()) which worked well...

What am I doing wrong in here?

There does not have to be a direct connection (via FKs e.g.) between Table 1 and Table 7, right?

Any suggestions?


Greets - Der Steps


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 06, 2008 4:20 pm 
Beginner
Beginner

Joined: Tue Sep 19, 2006 11:26 am
Posts: 33
The first line of your hql looks wrong to me:

Code:
from Table_1 table_1, Table_7 table_7, Table_5 table_5


I think your query should be:

Code:
from Table_5 table_5
inner join Table_6 as table_6 on table_5.Id = table_6.five_id.Id
inner join Table_4 as table_4 on table_5.Id = table_4.five_id.Id
inner join table_7 on table_6.seven_id.Id = table_7.id
inner join Table_3 as table_3 on table_4.three_id.Id = table_3.Id
inner join Table_2 as table_2 on table_3.Id = table_2.three_id.Id
inner join table_1 on table_2.one_id.Id = table_1.Id
where table_1.Id = :OneIDWhichIsPassedByMe


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 10, 2008 4:18 am 
Newbie

Joined: Thu Mar 06, 2008 8:27 am
Posts: 7
Hi there!

First: Thanks a lot for your answer. I managed to try it now, but the error still is the same...unfortunately...

I let MSSQL Server determine the SQL Statement for me again. Translated to the enumerated tables I used in my first posting, I have to do the following query, which I just can't translate myself, could you help me out with that?

Code:
SELECT DISTINCT
   dbo.table_5.desired_value_from_tbl_5, dbo.table_7.desired_value_from_tbl_7, dbo.table_3.desired_value_from_tbl_3, dbo.table_1.desired_value_from_tbl_1

FROM
   dbo.table_5
   
INNER JOIN
   dbo.table_4 ON dbo.table_5.id = dbo.table_4.fk_from_table_4_id
   
INNER JOIN
   dbo.table_3 ON dbo.table_4.fk_from_table_3_id = dbo.table_3.id
   
INNER JOIN
   dbo.table_2 ON dbo.table_3.id = dbo.table_2.fk_from_table_3_id
   
INNER JOIN
   dbo.table_6 ON dbo.table_5.id = dbo.table_6.fk_from_table_5_id
   
INNER JOIN
   dbo.table_7 ON dbo.table_6.fk_from_table_7 = dbo.table_7.id
   
INNER JOIN
   dbo.table_1 ON dbo.table_2.fk_from_table_1_id = dbo.table_1.id
   
WHERE     
   (dbo.table_1.id = myDesiredIdValue)

This query works well for me, but my translation to HQL does throw the error I mentioned above... here is what I tried, according to your reply:

Code:
from
   Table_5 as table_5
   
inner join
   Table_4 as table_4 on table_5.Id = table_4.FK.Id                       

inner join
   Table_3 as table_3 on table_4.FK.Id = table_3.Id

inner join
   Table_2 as table_2 on table_3.Id = table_2.FK.Id

inner join
   Table_6 as table_6 on table_5.Id = table_6.FK.Id                       

inner join
   Table_7 as table_7 on table_6.FK.Id = table_7.Id                       
   
inner join
   Table_1 as table_1 on table_2.FK.Id = table_1.Id                       

where table_1.Id = :myDesiredID

(FKs according to MSSQL-Query above, remember, I have to anonymize queries...).

What am I doing wrong? Do you need any further information?


Greets and thanks - Steps


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 10, 2008 9:11 am 
Beginner
Beginner

Joined: Tue Sep 19, 2006 11:26 am
Posts: 33
Sorry I didn't look further than the first line. I don't think you have translated from the SQL to the HQL properly.

It is a bit difficult to come up the with the HQL without the class definitions and mapping files.

Hopefully a simpler example will point you in the right direction.

TableA and TableB have a foreign key relationship such that TableA has many children of type TableB. This should result in a TableA class consisting of a property for each column in TableA plus A list - the objects in the list will be of type TableB, the children. You could then do an inner join on TableA and TableB with the following HQL:

Code:
from TableA A
inner join A.TableBList B
where B.Id = :val


As an aside, I tend to find using ICriteria objects to build up queries like this much easier than using hql.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 10, 2008 11:24 am 
Newbie

Joined: Thu Mar 06, 2008 8:27 am
Posts: 7
Hi!

Yes, I know I got it wrong...first time HQL for me...

I know about Criteria-Queries, but the architecture of the project I am working on forbids me to use them...

I came up with the following solution which returns exactly the results I wants it to return (

Code:
select distinct brand from ArticleInfoNm as articleInfoNm
                      inner join articleInfoNm.Article as article
                      inner join articleInfoNm.Info as info,
                      ArticleBrandSubBrandNm as articleBrandSubBrandNm
                      inner join articleBrandSubBrandNm.Brand as brand,
                      InfoSupplierNm as infoSupplierNm
                      inner join infoSupplierNm.Supplier as supplier
                     
                     
                      where (supplier.Id = :supplierId) AND (article.Id = articleBrandSubBrandNm.Article.Id) AND (info.Id = infoSupplierNm.Info.Id)


(As you can see I have been allowed to post the real table names by now, but the neither the structure of the underlying DB, nor the mappings themselves)

Thanks a lot - Steps


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.