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}
))
标签:postgresql,spring-data-jpa,jpa,spring,java 来源: https://codeday.me/bug/20191110/2014881.html