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}   
))

相关文章

项目需要,有个数据需要导入,拿到手一开始以为是mysql,结果...
本文小编为大家详细介绍“怎么查看PostgreSQL数据库中所有表...
错误现象问题原因这是在远程连接时pg_hba.conf文件没有配置正...
因本地资源有限,在公共测试环境搭建了PGsql环境,从数据库本...
wamp 环境 这个提示就是说你的版本低于10了。 先打印ph...
psycopg2.OperationalError: SSL SYSCALL error: EOF detect...