2009-05-28 63 views
0

我有一個可用的HQL查詢,我想優化。它如下:HQL查詢等價性:爲什麼它們不同

select distinct A.id from Import as A, Place D 
where (A.place=D or A.placeBOK=D) and D.country=? 

I tried to replcae the query from above by the following: 

select distinct A.id from Import as A 
where A.place.country=? or A.placeBOK.country=? 

除了性能,我認爲這兩個查詢是等價的。 但他們不是。第一個是發送一組20個對象,而第二個發送只有14個對象。

我在做什麼錯?

任何提示?

回答

1

[增訂]

你必須重寫你查詢到

select distinct A.id from Import as A LEFT JOIN A.place b LEFT JOIN A.placeBOK c 
where b.country=? or c.country=? 

你的第二個查詢的等效:

select distinct A.id from Import as A INNER JOIN A.place b INNER JOIN A.placeBOK c 
where b.country=? or c.country=? 

參見:

14.4. Forms of join syntax

HQL支持兩種形式的關聯連接:隱式和顯式。

上一節中顯示的查詢都使用顯式形式,其中在from子句中明確使用了join關鍵字。這是推薦的形式。

隱式表單不使用join關鍵字。相反,這些關聯是使用點符號「取消引用」的。隱式連接可以出現在任何HQL子句中。隱式連接導致生成的SQL語句中的內部連接。

from Cat as cat where cat.mate.name like '%s%' 
+0

很抱歉,但我不明白很好。他們是否等同? – Luixv 2009-05-28 14:36:37

0

我不明白上面的答案,但也許你可以試試調查結果6這是從查詢產生的而不是從查詢乙...