Hibernate生成一个;在…上和null=null";左联

Hibernate生成一个;在…上和null=null";左联,hibernate,postgresql,jpa,jpql,Hibernate,Postgresql,Jpa,Jpql,环境: hibernate entitymanager:4.3.6.4最终版本 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 查看日志文件,这将为Post

环境:

  • hibernate entitymanager:4.3.6.4最终版本
  • 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上运行注释
和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”条件添加到左连接


我在Oracle上试用过,得到了非常相同的结果。

不要使用
实体。road不是空的
直接使用您的别名
road

select distinct entity
from Incidence entity
left join treat(entity.road as Road) as road
where road is not null and      lower(road.nomenclature) like :value 
因为当您使用entity.road Hibernate尝试添加隐式连接时,在您的情况下,它可能会在您的左连接和它自己的隐式连接之间混淆


另外,如果你只想拥有一条具有特定命名法的道路,为什么不直接使用
连接而不是
左连接呢

来自JPA 2.1规范:4.4.9向下广播:

如果目标类型不是第一个参数的静态类型的子类型(正确或不正确),则查询无效

在我的模型中,Road是一个实体类,没有super类,也没有子类型,因此,正如我所理解的,我的JPQL应该抛出一个异常,或者(因为是同一类型)忽略treat表达式


经过几次证明后,我发现问题在于
treat
表达式的使用。我已经为此写了一篇文章。无论如何,谢谢你。