我正在尝试在jpa查询中使用in
子句,但它会出现Operand should contain 1 column(s)
错误。
这是我的问题:
@Query(value = "select e from Table e where " +
"((:plantId is null and :unitId is null and :functionalLocationId is null) or" +
" (:functionalLocationId is not null and e.functionalLocation.id in (select f.id from FunctionalLocation f where f.id = :functionalLocationId)) or" +
" (:unitId is not null and :functionalLocationId is null and e.functionalLocation.unit.id in (select u.id from Unit u where u.id = :unitId)) or" +
" (:plantId is not null and :unitId is null and :functionalLocationId is null and e.functionalLocation.unit.plant.id in (select p.id from Plant p where p.id = :plantId))) and" +
"((:equipmentTagNumbers) is null or e.tagNo in (:equipmentTagNumbers)) and" +
"(:startDate is null or e.lastUpdateDate >= :startDate) and" +
"(:endDate is null or e.lastUpdateDate <= :endDate)" +
"order by e.id desc")
:equipmentTagNumbers
属性是Lis<String>
,如果我为它发送null,则查询按预期工作,但是当我发送实际数据时,它会给出错误。
有什么建议吗?
答案 0 :(得分:2)
installed.tenants="yourparticulartenant"
成为
((:equipmentTagNumbers) is null or...
这不是正确的SQL。
相反,请这样:
(1,2,4 is null or...
这样,当设备为空时,equipmentTagNumbers不会影响查询。
答案 1 :(得分:0)
据我所知,如果您的参数在这种情况下是isEmpty
,则jpa查询中没有length
或array
。 @SOlsson是对的(1,2,4 is null or...
是无效的Sql所以我决定在我的查询中添加一个额外的参数,以便检查它是否null
,所以我的上一个查询如下:
...
"((:hasEquipmentNumbers) is null or e.tagNo in (:equipmentTagNumbers)) and"
...
:hasEquipmentNumbers
是一个布尔值,我可以指定null
,所以如果它为null则无人伤害,如果不是我可以毫无困难地运行我的IN
子句。