【问题标题】:JPQL query with left join searching for entity具有左连接搜索实体的 JPQL 查询
【发布时间】:2020-09-02 06:42:58
【问题描述】:

基于以下文章 JPQL, OR only returning result of one condition? 我创建了以下查询

Collection<Profile> profiles = dataManager.load(Profile.class).view("profile-view")
        .query("select p from userlifecyclemgmt_Profile p " +
                " left join p.legalEntities f where" + 
                " p.isAvailableForAll = true or :legalEntity MEMBER OF f" ) 
        .parameter("legalEntity", serviceRequest.getHrUser().getLegalEntity())
        .list();

参数:legalEntity 是类LegalEntity 的对象。这个查询给了我以下错误:

JpqlSyntaxException: Errors found for input jpql:[select p from userlifecyclemgmt_Profile p  left join p.legalEntities f where p.isAvailableForAll = true or :legalEntity MEMBER OF f]
CommonErrorNode [<unexpected: [@35,120:125='MEMBER',<113>,1:120], resync=:legalEntity MEMBER OF f>]

使用相同的查询稍作修改:

Collection<Profile> profiles = dataManager.load(Profile.class).view("profile-view")
        .query("select p from userlifecyclemgmt_Profile p " +
                " left join p.legalEntities f where" +
                " p.isAvailableForAll = true or :legalEntity MEMBER OF p.legalEntities" )
        .parameter("legalEntity", serviceRequest.getHrUser().getLegalEntity())
        .list();

没有错误,但只给了我OR 语句第二部分的结果。我知道 JPQL 在后台创建了一个INNER JOIN 查询,这就是原因。但是,如何让第一个查询起作用?

提前致谢!

【问题讨论】:

  • 我使用 2 个查询实现了一个解决方法,然后合并结果。但这并不是一个真正可以接受的解决方案。

标签: hibernate jpa jpql cuba-platform


【解决方案1】:

你应该这样修改查询

select distinct p 
from userlifecyclemgmt_Profile p left join p.legalEntities f 
where p.isAvailableForAll=true or f=:legalEntity

from userlifecyclemgmt_Profile p left join p.legalEntities f 表达式生成left join 本机查询,该查询返回纯结果profile-legalEntityf成为单个legalEntity的别名,不能在member of中使用,否则p.legalEntities可以。

select p 多次返回相同的配置文件 (legalEntities.size())。所以你需要distinct

【讨论】:

  • 这很好用。做得好!如果您能给我更多的解释,那就太好了。
猜你喜欢
  • 1970-01-01
  • 2019-06-05
  • 2012-10-25
  • 1970-01-01
  • 2012-02-08
  • 2014-10-26
  • 2020-10-06
  • 2014-02-21
  • 1970-01-01
相关资源
最近更新 更多