我想为下面的SQL语句创建一个JPA参数化查询
select * from car where (colour, speed) in (('red', 50), ('blue', 70))
这个查询返回预期的结果
entityManager.createQuery("from Car c where (c.colour, c.speed) in (('red', 50), ('blue', 70))", Car.class).getResultList();
如何将当前硬编码的值作为参数传递?
我想出了下面的"工作"解决方案。但是我担心不能保证所有的参数对都按照预期的顺序传递。我不想要速度为50的"蓝色"车。
edit:删除,因为它不像预期的那样工作,另见@Gas注释
在搜索时,我在这里发现了一个非常相似的问题。也许那更符合这个问题。
但是,我实现了一些稍微不同的东西。
@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
);