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

Hibernate HQL查询等价性:为什么它们不同,hibernate,hql,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

我有一个工作的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个对象

我做错了什么


有什么提示吗?

[更新]

您必须将查询重写为

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=?
另见:

HQL支持两种形式的关联联接:隐式和显式

上一节中显示的查询都使用显式形式,其中在from子句中显式使用join关键字。这是推荐的表格

隐式表单不使用join关键字。相反,使用点表示法“取消引用”关联。隐式联接可以出现在任何HQL子句中。隐式联接在生成的SQL语句中产生内部联接

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

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

对不起,我不太明白。它们是否相等?我更正了我的答案(希望它会更清楚)。关键的一点是,您必须在内部联接上执行左联接,这是“.”语法的隐式行为。