环境:
休眠实体管理器:4.3.6.Final
PostgreSQL 9.3.5版
我执行当前的这个JPQL查询

select distinct entity
from Incidence entity
  left join treat(entity.road as Road) as road
where entity.road is not null and lower(road.nomenclature) like :value

在日志文件中,这将为PostgreSQL生成以下查询:
select
distinct
....
....
from public.incidence incidence0_
left outer join public.road road1_
    on incidence0_.road=road1_.id and null=null
where (incidence0_.road is not null)  and (lower(road1_.nomenclature) like ? )

使用%cv%作为参数,这个查询应该返回175行,但我没有得到任何结果。
如果我在PostgreSQL上运行注释and null=null的查询,就会得到预期的结果:
select
distinct
....
....
from public.incidence incidence0_
left outer join public.road road1_
    on incidence0_.road=road1_.id /* and null=null */
where (incidence0_.road is not null)  and (lower(road1_.nomenclature) like '%cv%' )

所以。。。为什么hibernate在左连接中添加“null=null”条件?
我和甲骨文试过了,结果也一样。

最佳答案

经过几次证明,我发现问题在于treat表达式的使用。
来自JPA 2.1规范:4.4.9下浇:
...
如果目标类型不是第一个参数的静态类型的子类型(正确或不正确),则查询无效。
在我的模型中,Road是一个没有super类的实体类,并且没有子类型,因此,正如我所理解的,我的JPQL应该抛出一个异常或者忽略treat表达式
我已经创造了一个关于它的Jira request

10-02 00:47
查看更多