微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java-spring数据jpa和多次收集的条件

我想通过所有应该匹配的列表.

  @Query("select g.name as name FROM Gym  g  " +
            "LEFT JOIN g.listofEquipment   listofEquipment  " +
            "WHERE " +
            "(((:listofEquipment) is null) or listofEquipment.id in (:listofEquipment)) "+
            "AND (((:listofAmenity) is null) or listofAmenity.id in (:listofAmenity))")
  Page<Map<String, Object>> listing(@Param("listofEquipment") Set<Integer> listofEquipment,@Param("listofAmenity") Set<Integer> listofAmenity)

以上查询工作为OR,但我需要AND

假设我通过1,2,3
那么结果应该是所有拥有设备1、2和3的体育馆
便利设施也一样

我试过了
Finding items with a set containing all elements of a given set with jpql

但它没有帮助我,因为有多个过滤器

网址为

http://localhost:8080/filter?listofEquipment=5,3&listofAmenity=51,13&sort=name

解决方法:

查询应为您提供匹配所有listofEquipments的Gym.ids,并假定该列表不包含重复项.

select g.id as name 
FROM Gym  g 
LEFT JOIN g.listofEquipment listofEquipment
WHERE listofEquipment.id in (:listofEquipment)) 
GROUP BY g.id
HAVING count(g.id) = #{#listofEquipment.size}    

您可以将它和对listofAmenity的类似查询用作主查询中的子选择.

select g.name as name 
FROM Gym  g 
LEFT JOIN g.listofEquipment listofEquipment 
WHERE (((:listofEquipment) is null) 
    OR g.id in (
    select g.id as name 
    FROM Gym  g 
    LEFT JOIN g.listofEquipment listofEquipment
    WHERE listofEquipment.id in (:listofEquipment)) 
    GROUP BY g.id
    HAVING count(g.id) = #{#listofEquipment.size}   
)) 
AND (((:listofAmenity) is null) 
    OR g.id in (
    select g.id as name 
    FROM Gym  g 
    LEFT JOIN g.listofAmenity listofAmenity
    WHERE listofAmenity.id in (:listofAmenity)) 
    GROUP BY g.id
    HAVING count(g.id) = #{#listofAmenity.size}   
))

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐