JPQL中的多个IN条件

时间:2010-11-30 12:28:06

标签: java jpa jpql

如何在JPQL中表达以下SQL:

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

BTW:以上是有效的Oracle SQL语法

2 个答案:

答案 0 :(得分: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)

它不漂亮。

答案 1 :(得分:0)

编辑:忘记后面的内容,这是不正确的。留下来表达思考

我认为你首先必须将多维声明分成它的成分:

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

将在JPQL中转换(假设“table”映射到实体TableDto),如下所示:

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

上述内容未经测试,但可以在JPQL reference documentation

中找到更多信息
相关问题