【问题标题】:multiple IN conditions in JPQLJPQL 中的多个 IN 条件
【发布时间】:2023-03-29 11:40:01
【问题描述】:

在 JPQL 中如何表达如下 SQL:

select * from table where 
( ( table.col1 , table.col2) in 
   (col1val1, col2val1),
   (col1val2, col2val2),
   (col1val3, col2val3)
)

顺便说一句:以上是有效的 Oracle SQL 语法

【问题讨论】:

    标签: java jpa jpql


    【解决方案1】:

    我的 JPQL 很糟糕,但是如何:

    select tableDto from TableDto tableDto 
    where (tableDto.col1 = col1val1 and tableDto.col2 = col2val1)
    or (tableDto.col1 = col1val2 and tableDto.col2 = col2val2)
    or (tableDto.col1 = col1val3 and tableDto.col2 = col2val3)
    

    不好看。

    【讨论】:

      【解决方案2】:

      编辑:忘记下面的内容,这是不正确的。留下来展示想法

      我认为您首先必须将多维语句拆分为其组成部分:

      select * from table 
      where table.col1 in (col1val1, col1val2, col1val3)
      and table.col2 in (col2val1, col2val2, col2val3)
      

      这将在 JPQL 中翻译(假设“表”映射到实体 TableDto),如下所示:

      select tableDto from TableDto tableDto 
      where tableDto.col1 in(col1val1, col1val2, col1val3)
      and tableDto.col2 in(col2val1, col2val2, col2val3)
      

      以上内容未经测试,但可以在JPQL reference documentation中找到更多信息。

      【讨论】:

      • 问题在于该查询将匹配 (col1val1, col2val2),而原始查询不会。
      猜你喜欢
      • 2012-06-21
      • 2013-07-09
      • 2012-06-08
      • 1970-01-01
      • 1970-01-01
      • 2012-01-22
      • 1970-01-01
      • 2021-11-13
      • 1970-01-01
      相关资源
      最近更新 更多