JPA查询与生成的SQL查询不同

时间:2019-02-22 13:50:39

标签: hibernate spring-data-jpa

我在Spring存储库中有这个JPA查询定义

  @Query(value="select rd.contractID as contractID, rd.gicCode as gicCode, count(rd) as ticketNo" 
      + " from RawData rd" 
      + " where rd.contractID is not null and ((rd.contractCostObject is null and rd.domestic = true) or (rd.mirrorSVO is null and rd.domestic = false))"
      + " group by rd.contractID, rd.gicCode")

但是生成的SQL查询就是那个

select
    rawdata0_.contractID as col_0_0_,
    rawdata0_.gicCode as col_1_0_,
    count(rawdata0_.id) as col_2_0_ 
from
    RawData rawdata0_ 
where
    (
        rawdata0_.contractID is not null
    ) 
    and (
        (
            rawdata0_.contractCostObject is null
        ) 
        and rawdata0_.domestic=true 
        or (
            rawdata0_.mirrorSVO is null
        ) 
        and rawdata0_.domestic=false
    ) 
group by
    rawdata0_.contractID ,
    rawdata0_.gicCode

您可以看到方括号已更改,并且我相信两个查询中的条件并不相同。 是虫子吗? 在带有postgresql数据库的spring上使用hibernate。

1 个答案:

答案 0 :(得分:0)

[core] repositoryformatversion = 0 filemode = false bare = false logallrefupdates = true symlinks = false ignorecase = true [remote "origin"] url = my ssh url fetch = +refs/heads/*:refs/remotes/origin/* [branch "master"] remote = origin merge = refs/heads/master 运算符的优先级高于and运算符。

  • 条件A和B或C和D

等同于

  • (条件A和B)或(C和D)

将此规则应用于您的查询,实际结果类似于:

or

正是您想要的。 JPA生成的查询是正确的。