HQL查询等价:为什么它们不同

时间:2009-05-28 11:22:12

标签: hibernate 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个物体。

我做错了什么?

任何提示?

2 个答案:

答案 0 :(得分: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%'

答案 1 :(得分:0)

我不明白上面的答案,但也许您可以尝试调查由查询A产生的6个结果,但不是来自查询B ......