I want to create a JPA parameterised query for following SQL statement
select * from car where (colour, speed) in ((\'red\', 50), (\'blue\', 70))
While searching, I found a very similar question here. Perhaps that's more in line with this problem.
However, I implemented something slightly different.
@Query("SELECT p FROM Product p "
+ "LEFT JOIN p.categories category "
+ "WHERE UPPER(p.name) LIKE UPPER(CONCAT('%', COALESCE(:searchRequest, ''), '%')) "
+ "AND UPPER(p.description) LIKE UPPER(CONCAT('%', COALESCE(:description, ''), '%')) "
+ "AND p.price BETWEEN :priceLow AND :priceHigh "
+ "AND p.averageRating >= :averageRating "
+ "AND p.archived = :archived "
+ "AND ((category.name IN :selectedCategories) "
+ "OR (:amountOfSelectedCategories = 0 AND category IN (SELECT c FROM Category c))) "
+ "GROUP BY p "
+ "HAVING SIZE(p.categories) >= :amountOfSelectedCategories"
)
Page<Product> findAllBySearchModel(
Pageable pageable,
@Param("searchRequest") String searchRequest,
@Param("description") String description,
@Param("priceLow") BigDecimal priceLow,
@Param("priceHigh") BigDecimal priceHigh,
@Param("averageRating") double averageRating,
@Param("archived") boolean archived,
@Param("selectedCategories") List<String> selectedCategories,
@Param("amountOfSelectedCategories") int amountOfSelectedCategories
);